开发者

How to use the ($) operator correctly

These lines execute correctly:

Prelude> 1 / (1 + 1)
0.5
Prelude> (/) 1 $ (+) 1 1
0.5
Prelude> (/) 1 $ 1 + 1
0.5

This one does not:开发者_如何学JAVA

Prelude> 1 / $ (+) 1 1

<interactive>:1:4: parse error on input `$'

Why?


/ is an infix operator. It requires valid expression on both its sides. 1 is a literal and thus a valid expression. However, on right-hand side you have immediately another infix operator, which requires to be preceded by another valid expression (and 1 / is not a valid expression, as it lacks right-hand side argument to the / operator). This is why parser reports error (invalid grammar — see haskell report for ugly details ;)


I believe that it is because $ is an operator that requires a function preceding it. The expression 1 / in your last example does not evaluate to a function. In that case, the parser is expecting to find a (numeric) expression as the second argument to the / operator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜