简体   繁体   English

如何正确使用($)运算符

[英]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: 这不是:

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. 1是文字,因此是有效的表达式。 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). 但是,在右侧,您将立即拥有另一个infix运算符,该运算符必须在其前面加上另一个有效的表达式(并且1 /不是有效的表达式,因为它缺少/运算符的右侧参数)。 This is why parser reports error (invalid grammar — see haskell report for ugly details ;) 这就是解析器报告错误的原因(语法无效-有关详细信息,请参见haskell报告;)

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. 在上一个示例中,表达式1 /不会求值函数。 In that case, the parser is expecting to find a (numeric) expression as the second argument to the / operator. 在这种情况下,解析器期望找到一个(数字)表达式作为/运算符的第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM