“。”之间的差异。和“$”表达式 [英] The difference between "." and "$" expressions

查看:129
本文介绍了“。”之间的差异。和“$”表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

Haskell:区别。 (点)和$(美元符号)


据我所知,$将一个像f1(f2 x)这样的表达式绑定到一个更简单的形式f1 $ f2 x。 f1.f2 x与这个有什么不同?

好吧, f1。 f2 x f1 f2 x 的组合。你可能意指(f1。f2)x ,或者等价地, f1。 f2 $ x ;即应用于 x f1 f2

通过查看类型可以找到答案:

 ($)::(a  - > b) - > a  - > b 
(。)::(b - > c) - > (a - > b) - > a - > c

简单地说,($)函数的参数, 1 (。) 构成了它的两个论点。两条链

  f。 G 。 h $ x 

  f $ g $ h $ x 

是等价的;前者通常是首选,因为使用剪切和粘贴将构图重构为自己的功能更容易,但这不是普遍的偏好。它的视觉噪音也稍低。



1 确实,($) id ,身份函数相同;它只是返回给定的函数。它只是有用的,因为运营商的优先级很低(实际上是最低的)。


Possible Duplicate:
Haskell: difference between . (dot) and $ (dollar sign)

I understand that "$" binds an expression like "f1 (f2 x)" to a simpler form "f1 $ f2 x". How does "f1.f2 x" differ from this?

解决方案

Well, f1 . f2 x is the composition of f1 and f2 x. You probably mean (f1 . f2) x or, equivalently, f1 . f2 $ x; i.e. the composition of f1 and f2, applied to x.

The answer can be found by looking at the types:

($) :: (a -> b) -> a -> b
(.) :: (b -> c) -> (a -> b) -> a -> c

Simply, ($) applies a function to its argument,1 and (.) composes its two arguments. The two chains

f . g . h $ x

and

f $ g $ h $ x

are equivalent; the former is generally preferred because it's easier to refactor the composition out into its own function with cut-and-paste, but this isn't a universal preference. It's also a bit lower on visual noise.

1 Indeed, ($) is identical to id, the identity function; it just returns the function it's given. It's only useful because of the very low operator precedence (the lowest, in fact) given to it.

这篇关于“。”之间的差异。和“$”表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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