[英]How does function application with the $ operator curry in Haskell?
我正在学习haskell并且有点困惑函数应用程序运算符$ curry的。
根据GHC,$的类型是
*Main>:t ($)
($) :: (a->b) -> a -> b
但我可以输入以下代码
*Main>map ($ 2) [(*2), (+2), (/2)]
[4.0,4.0,1.0]
根据$的签名,虽然我认为我需要使用翻转函数,因为$的第一个参数是(a-> b)。
例如,我不能做以下事情
curry_test :: Integer -> String -> String
curry_test x y = (show x) ++ " " ++ y
*Main> let x = curry_test "123"
Couldn't match expected type `Integer' with actual type `[Char]'
In the first argument of `curry_test', namely `"123"'
In the expression: curry_test "123"
In an equation for `x': x = curry_test "123"
但我能做到
let x = curry_test 2
中缀运营商有特殊规则。 请参阅此页: http : //www.haskell.org/haskellwiki/Section_of_an_infix_operator
基本上,由于$
是一个中缀运算符, ($ 2)
实际上将2
固定为$
的第二个参数,所以它相当于flip ($) 2
。
这个想法是使操作员的部分应用程序更直观,因此,例如,如果您在列表上map (/ 2)
,您可以想象将列表的每个元素放在除法符号左侧的“缺失”点。
如果你想以这种方式使用curry_test
函数,你可以这样做
let x = (`curry_test` "123")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.