[erlang-questions] comma vs andalso

Richard O'Keefe ok@REDACTED
Tue Jul 7 02:47:23 CEST 2009


On Jul 7, 2009, at 12:19 AM, Roberto Ostinelli wrote:
> the difference is very simple. comma indicates that both guard  
> operators need to be evaluated, while on the contrary 'andalso'  
> means that the second condition is evaluated ONLY if condition1 is  
> true. same goes for 'orselse'.

He's wrong, but he's not _crazy_.
Section 6.24 of the reference manual _ought_ to explain this
clearly, but doesn't.

--------------------------
6.24 Guard Sequences
A guard sequence is a sequence of guards,
separated by semicolon (;).
The guard sequence is true if at least one of the guards is true.
(The remaining guards, if any, will not be evaluated.)
Guard1;...;GuardK

A guard is a sequence of guard expressions,
separated by comma (,).
The guard is true if all guard expressions evaluate to true.
GuardExpr1,...,GuardExprN

--------------------------
I propose rewriting these paragraphs something like this:

A guard sequence is a sequence (G1;...;Gk) of guards
separated by semicolons.  Guards are tested from left
to right.  If some guard succeeds, the guard sequence
as a whole succeeds, and the remaining guards will not
be checked.  If all guards fail, the guard sequence as
a whole fails.

A guard is a sequence (GE1,...,GEn) of guard
expressions separated by commas.  Guard expressions
are tested from left to right.  If some guard
expression fails, the guard as a whole fails, and the
remaining guard expressions will not be checked.  If
all guard expressions succeed, the guard as a whole
suceeds.

A guard expression is a very special restricted
expression.  It is said to succeed it it evaluates to
true.  It is said to fail if it evaluates to anything
else or raises an exceptions.  Exceptions never
propagate out of guard expressions, they are just taken
as lack of success.

(then continue with the existing third paragraph)




More information about the erlang-questions mailing list