可能的重复:
哈斯克尔: 之间的区别。 (点)和 $(美元符号) https://stackoverflow.com/questions/940382/haskell-difference-between-dot-and-dollar-sign
我知道“$”将“f1 (f2 x)”这样的表达式绑定到更简单的形式“f1 $ f2 x”。 “f1.f2 x”与此有何不同?
Well, f1 . f2 x
的组成是f1
and f2 x
。你可能的意思是(f1 . f2) x
或者,等价地,f1 . f2 $ x
;即组成f1
and f2
, 应用于x
.
通过查看类型可以找到答案:
($) :: (a -> b) -> a -> b
(.) :: (b -> c) -> (a -> b) -> a -> c
Simply, ($)
applies a function to its argument,1 and (.)
composes http://en.wikipedia.org/wiki/Function_composition its two arguments. The two chains
f . g . h $ x
and
f $ g $ h $ x
是等价的;前者通常是首选,因为它更容易通过剪切和粘贴将组合重构为自己的功能,但这并不是普遍的偏好。视觉噪音也低一些。
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)