1
votes

I'm reading up on Haskell and as expected there are some operators that are left associative and others that are right associative. That got me thinking, how would the implementation differ for a new operator ¤, when comparing the left associative version versus the right associative version?

So given [1,2] ¤ [3,4] ¤ [5,6], what changes in the implementation if I want this to be interpreted as (([1,2] ¤ [3,4]) ¤ [5,6]) versus ([1,2] ¤ ([3,4] ¤ [5,6]))?

1

1 Answers

2
votes

To state that an operator is left- or right-associative, one can use a fixity declaration.

For instance

infixl 5 .+.                  -- left associative
infixr 5 .-.                  -- right associative
infix  5 .=.                  -- not associative

The last one causes x .=. y .=. z to be a parse error, requiring explicit parentheses.

The number 5 above is the precedence level, and should be chosen following the level of other operators (e.g. (+) is level 6, while ($) has level 0).