[Haskell-cafe] Why are && and || right-associative?
Ivan Perez
ivanperezdominguez at gmail.com
Fri Apr 12 02:26:44 UTC 2019
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.
If it is right associative:
a && b && c = a && (b && c), which means going into a, and if it is False,
you are done.
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.
Just a guess. But you got me interested now.
Does anyone have the real explanation?
Cheers,
Ivan
On Thu, 11 Apr 2019 at 22:13, Richard Eisenberg <rae at richarde.dev> wrote:
> Hi cafĂ©,
>
> Why are && and || in the Prelude right-associative? This contradicts my
> expectation and the way these work in other languages. That said, I can't
> think of any harm in it. This came up from a question asked by a student,
> and I have no idea why the design is this way.
>
> Thanks,
> Richard
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190411/2bc0b651/attachment.html>
More information about the Haskell-Cafe
mailing list