[Haskell-cafe] Why are && and || right-associative?

Albert Y. C. Lai trebla at vex.net
Fri Apr 19 17:31:09 UTC 2019


On 2019-04-12 10:47 a.m., Richard Eisenberg wrote:
> If you look at definitions of other languages (C, Java), you see that 
> the operators are defined to be left-associative. Perhaps those other 
> languages got it wrong, then. :)
> 
> In any case, this conversation has been illuminating. Thanks!

I am late to this discussion but here is my solution.

This is really just story-telling to end-users.

The real story you want to tell everyone is this: "x && y && z && t" 
means Scheme's "(and x y z t)", and it means you try the sequence from 
left to right, stopping at the first incident of "false".

To those people who evaluate an AST in the bottom-up order, e.g., C 
programmers, this sounds like left-associating because the left is more 
likely evaluated so these people need to envision a left-leaning AST. 
So you comfort them with "yeah!".

To those people who evaluate an AST in the top-down order, e.g., Haskell 
programmers, this sounds like right-associating because the right is 
more likely skipped so these people need to envision a right-leaning 
AST.  So you comfort them with "yeah!".

So either (both C and Haskell are right) or (both C and Haskell are wrong).

As many of you have observed, it doesn't matter, a compiler writer 
already knows it's "(and x y z t)" and generates the correct code and 
not bother to split hair.


More information about the Haskell-Cafe mailing list