[erlang-questions] Reading, Learning, Confused

Richard Carlsson <>
Sat Jul 19 17:15:27 CEST 2008


Alpár Jüttner wrote:
> Btw. the Erlang Reference Manual says that
> 
>         As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are
>         allowed in guards. In guards, however, evaluation is always
>         short-circuited since guard tests are known to be free of side
>         effects.
>         (Section 6.14, Short-Circuit Boolean Expressions)
> 
> Something is wrong here, isn;t it?

Not really, but it's a bit cryptic. The text only talks about evaluation 
of guard tests, i.e., the complete expressions separated by commas 
and/or semicolons. The commas/semicolons are always short-circuited. But 
they cannot be nested arbitrarily (you can have semicolon-separated 
groups of comma-separated expressions, but not the other way around and 
not grouped using parentheses). To write more complex expressions and 
still have short-circuiting, you need andalso/orelse.

The strictness of the and/or operators is historical; I think the 
reasoning was that evaluating both sides gave a more predictable 
behaviour, with regard to possible exceptions and evaluation time. This 
was probably a mistake, but it was too late to fix, so andalso/orelse 
were added.

The short answer to "when would anyone want to use plain and/or?" is 
"rarely". A slightly longer answer would be "when readability and 
horizontal space is more important than shaving off a few cycles".

     /Richard



More information about the erlang-questions mailing list