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

Viktor Dukhovni ietf-dane at dukhovni.org
Fri Apr 12 23:16:12 UTC 2019


> On Apr 12, 2019, at 3:15 PM, Joachim Durchholz <jo at durchholz.org> wrote:
> 
> What is the basis for this expectation?
> My expectation would be left associativity, just because I read stuff from left to right, and left associativity is what I get if I mentally parse from left to right.
> So I'm curious what thought process arrives at the opposite expectation.

Since (&&) short-circuits on first failure, and (||) short-circuits on
first success, right associativity is more intuitive:

	a && b && c == a && (b && c)
	a || b || c == a || (b || c)

as each can stop immediately when `a` is respectively False or True.
This matches the fact that (&&) is naturally strict in its first argument
and and lazy in the second, which works well with currying.

Also consider that, for example, in "ghci":

	foldr (&&) True (replicate 100000000000 False)

returns immediately, while:

	foldr (&&) True (replicate 100000000000 False)

hangs, which clearly shows that right folds are the more
natural way to combine these boolean operators.

And reading left to right, *is* IMHO right associative!  You 
see:

	a || ...

and immediately process a, then move on to what follows.

When reading an English sentence, you don't have to reach
the last word before you can start to make sense of the
preceding words, for left associativity, try German...

[ Oops! Never mind, perhaps that explains the difference
  in perspective. :-)  :-) ]

-- 
	Viktor.



More information about the Haskell-Cafe mailing list