Haskell 组合 (.) 与 F# 的管道转发运算符 (|>) [英] Haskell composition (.) vs F#'s pipe forward operator (|>)

查看:27
本文介绍了Haskell 组合 (.) 与 F# 的管道转发运算符 (|>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 F# 中,管道转发运算符 |> 的使用非常普遍.但是,在 Haskell 中,我只见过使用函数组合 (.).我知道它们相关,但是有没有在 Haskell 中使用管道转发的语言原因还是其他原因?

解决方案

我有点怀疑...

文化:我认为 |> 是 F#文化"中的一个重要操作符,对于 Haskell 来说,可能与 . 类似.F# 有一个函数组合运算符 << 但我认为 F# 社区倾向于使用 无积分风格 少于 Haskell 社区.

语言差异:我对这两种语言的了解不够,无法进行比较,但也许概括 let 绑定的规则差异很大,会影响到这一点.例如,我知道在 F# 中有时会写

让 f = exp

不会编译,你需要显式的 eta 转换:

let f x = (exp) x//或 x |>经验值

使其编译.这也引导人们远离无点/组合风格,转向流水线风格.此外,F# 类型推断有时需要流水线操作,因此已知类型会出现在左侧(参见 这里).

(就我个人而言,我觉得无积分风格不可读,但我想每一个新的/不同的东西在你习惯之前似乎都是不可读的.)

我认为这两种语言都可能适用于任何一种语言,历史/文化/事故可能会定义为什么每个社区都定居在不同的吸引者"上.

In F#, use of the the pipe-forward operator, |>, is pretty common. However, in Haskell I've only ever seen function composition, (.), being used. I understand that they are related, but is there a language reason that pipe-forward isn't used in Haskell or is it something else?

解决方案

I am being a little speculative...

Culture: I think |> is an important operator in the F# "culture", and perhaps similarly with . for Haskell. F# has a function composition operator << but I think the F# community tends to use points-free style less than the Haskell community.

Language differences: I don't know enough about both languages to compare, but perhaps the rules for generalizing let-bindings are sufficiently different as to affect this. For example, I know in F# sometimes writing

let f = exp

will not compile, and you need explicit eta-conversion:

let f x = (exp) x   // or x |> exp

to make it compile. This also steers people away from points-free/compositional style, and towards the pipelining style. Also, F# type inference sometimes demands pipelining, so that a known type appears on the left (see here).

(Personally, I find points-free style unreadable, but I suppose every new/different thing seems unreadable until you become accustomed to it.)

I think both are potentially viable in either language, and history/culture/accident may define why each community settled at a different "attractor".

这篇关于Haskell 组合 (.) 与 F# 的管道转发运算符 (|>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆