简体   繁体   English

带$的中缀函数评估

[英]Infix function evaluation with $

$ is an infix operator with the lowest possible precedence: $是具有最低优先级的中缀运算符:

f $ a = fa f $ a = fa

Does this not mean that, in the expression below, the part 这不表示在下面的表达式中

$ 2 ^ 2 $ 2 ^ 2

should be evaluated first and then add $ 2? 应该先评估后再加$ 2? It appears 2 + 2 is evaluated first 似乎2 + 2被首先评估

Prelude> (2^) $ 2 + 2 前奏>(2 ^)$ 2 + 2

returns : 返回:

16 16

No. Try to think of precedence not as being about what gets "evaluated first", and more about where parentheses are inserted. 不能。请优先考虑优先级,而不是优先考虑“先评估”的内容,而更多地考虑插入括号的位置。

The fact that $ has the lowest precedence means that parentheses are inserted around everything to the right of it (and, separately, to the left of it, if needed, but they aren't needed here). $具有最低的优先级这一事实意味着,括号会插入在其右侧的所有内容周围(如果需要,也可以在其左侧单独插入,但此处不需要)。 So 所以

(2^) $ 2 + 2 (2 ^)$ 2 + 2

is equivalent to 相当于

(2^) $ (2 + 2) (2 ^)$(2 + 2)

which is of course 当然是

(2^) 4 (ie 16) (2 ^)4(即16)

Precedence rules can be confusing, but I like to think of it as "lower precedence" means "do later". 优先级规则可能会造成混淆,但是我喜欢将其视为“较低优先级”表示“稍后再执行”。 Since $ has the lowest precedence (for example, below (+) ), it is performed after (+) . 由于$的优先级最低(例如,低于(+) ),因此它 (+) 之后执行。 Thus (2^) $ 2 + 2 evaluates (2^) to a partially applied function, then evaluates 2+2 to 4 , then applies 4 to 2^ to get 16 . 因此(2^) $ 2 + 2 (2^)评估为部分应用的函数,然后将2+2评估为4 ,然后将4应用于2^得到16

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

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