Without any parentheses :
Prelude> [1,2] >>= \n -> ['a', 'b'] >>= \ch -> return (n, ch)
[(1,'a'),(1,'b'),(2,'a'),(2,'b')]
Parentheses assuming left associativity:
Prelude> ([1,2] >>= \n -> ['a', 'b']) >>= \ch -> return (n, ch)
<interactive>:22:49: Not in scope: `n'
Parentheses assuming right associativity:
Prelude> [1,2] >>= (\n -> ['a', 'b'] >>= \ch -> return (n, ch))
[(1,'a'),(1,'b'),(2,'a'),(2,'b')]
Isn't >>=
left associative? When no parentheses are present, why does GHCi evaluate the expression as if >>=
is right associative?
Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first.
Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left. For example: '*' and '/' have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.
Nevertheless function composition in Haskell is right associative: infixr 9 .
Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).
Yes, >>=
is left associative. However, lambdas extend as far as possible. So the presence of the \n ->
means that the only correct way to parse the expression is as
[1,2] >>= (\n -> ['a', 'b'] >>= \ch -> return (n, ch))
Note that your "left associativity" form
([1,2] >>= \n -> ['a', 'b']) >>= \ch -> return (n, ch)
isn't even scope-correct. The n
in the final return
is out of scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With