简体   繁体   中英

Infix function evaluation with $

$ is an infix operator with the lowest possible precedence:

f $ a = fa

Does this not mean that, in the expression below, the part

$ 2 ^ 2

should be evaluated first and then add $ 2? It appears 2 + 2 is evaluated first

Prelude> (2^) $ 2 + 2

returns :

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

is equivalent to

(2^) $ (2 + 2)

which is of course

(2^) 4 (ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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