[英]How does function application with the $ operator curry in Haskell?
I am learning haskell and am a little confused how the function application operator $ curry's. 我正在学习haskell并且有点困惑函数应用程序运算符$ curry的。
According to GHC the type of $ is 根据GHC,$的类型是
*Main>:t ($)
($) :: (a->b) -> a -> b
But I can type the following code 但我可以输入以下代码
*Main>map ($ 2) [(*2), (+2), (/2)]
[4.0,4.0,1.0]
According to the signature of $ though I would assume I would need to use the flip function because the first parameter to $ is (a->b). 根据$的签名,虽然我认为我需要使用翻转函数,因为$的第一个参数是(a-> b)。
For example, I can't do the following 例如,我不能做以下事情
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"
But I can do 但我能做到
let x = curry_test 2
Infix operators have special rules. 中缀运营商有特殊规则。 See this page: http://www.haskell.org/haskellwiki/Section_of_an_infix_operator
请参阅此页: http : //www.haskell.org/haskellwiki/Section_of_an_infix_operator
Basically, since $
is an infix operator, ($ 2)
actually fixes 2
as the second argument of $
, so it is equivalent to flip ($) 2
. 基本上,由于
$
是一个中缀运算符, ($ 2)
实际上将2
固定为$
的第二个参数,所以它相当于flip ($) 2
。
The idea is to make partial application with operators more intuitive, so for example if you map (/ 2)
over a list, you can imagine putting each element of the list in the "missing" spot on the left side of the division sign. 这个想法是使操作员的部分应用程序更直观,因此,例如,如果您在列表上
map (/ 2)
,您可以想象将列表的每个元素放在除法符号左侧的“缺失”点。
If you want to use your curry_test
function this way, you could do 如果你想以这种方式使用
curry_test
函数,你可以这样做
let x = (`curry_test` "123")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.