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

Joachim Durchholz jo at durchholz.org
Fri Apr 12 04:42:33 UTC 2019


Am 12.04.19 um 04:26 schrieb Ivan Perez:
> Could it be so that you can shortcut in the expected order (left to right)?
> 
> Left associative:
> a && b && c = (a && b) && c which means going into a && b, which means 
> going into a, and if it is False, then going up in the expression tree.

For compile-time evaluation of known-to-be-constant values, this is what 
would indeed happen, but it wouldn't matter because such evaluation is 
done O(1) times.
Generated code will simply evaluate the conditions one after the other 
and abort as soon as it sees False.

> If you have a conjunction of n booleans, the complexity of evaluating
> this expression is linear with respect to the position of the first 
> False (in the case of &&). In the left-associative case, it is linear
> in the number of &&s.
This isn't the case.


More information about the Haskell-Cafe mailing list