גלעד ברקן
גלעד ברקן

Reputation: 23955

operator precedence in Haskell

I am confused about the rules for operator precedence in Haskell.
More specifically, why is this:

*Main> 2 * 3 `mod` 2
0

different than this?

*Main> 2 * mod 3 2
2

Upvotes: 7

Views: 1724

Answers (2)

Pubby
Pubby

Reputation: 53097

Function calls bind the tightest, and so

2 * mod 3 2

is the same as

2 * (mod 3 2)

Keep in mind that mod is not being used as an operator here since there are no backticks.

Now, when mod is used in infix form it has a precedence of 7, which (*) also has. Since they have the same precendence, and are left-associative, they are simply parsed from left to right:

(2 * 3) `mod` 2

Upvotes: 15

eazar001
eazar001

Reputation: 1601

2*3 = 6 and then mod 2 = 3 with no remainder ... so 6 mod 2 = 0 is your answer there. In your second case you are doing 2 * the result of mod 3 2 which is 2 * 1 = 2. Therefore your answer is 2.... Your operator precedence remains the same, you just arranged it so the answers were expressed accordingly.

Upvotes: 2

Related Questions