[erlang-questions] guard sequences problem

Richard A. O'Keefe <>
Thu Jun 19 01:00:23 CEST 2008


On 18 Jun 2008, at 11:47 pm, Circular Function wrote:
> 36> 5<4,12<2,12>4.
> true

This evaluates  5 < 4 and throws the result away; it
then evaluates 12 < 2 and throws the result away; it
last evaluates 12 > 4 and returns a result.

>
>
> 37> 5<4;12<2;12>4.
> * 1: syntax error before: 12
> 37>
>
This is indeed a syntax error.
You cannot use ';' to separate EXPRESSIONS.

> so apparently when separating with , it just evaluates the last  
> expression,

No, EXPRESSIONS separated by commas are ALL evaluated.
The RESULT is the result of the last expression.

> or at leats only returns the result of that.

That's it.  The comma operation in Erlang expressions is
*exactly* the same as the comma operation in C expressions.

In GUARDS, the comma is like a C "&&".

>
> and if i understand the manual right guard sequences
> should be written like that but apparently not.

Yes, but neither of your examples is a GUARD.

>
> first expression is short for
> 40> begin 4<2, 3>2 end.
> true
> ?

Obviously not.  The first expression has two commas
and includes 12 twice; this one has one comma, and
includes 12 not at all.

Part 1 of the old Erlang book is available from www.erlang.org.
Joe Armstrong's new Erlang book is well worth a read.
Several reads, in fact.
And of course the reference manual is on-line.

It would be a really good idea to put spaces around the
comparison operators, "4<2" is unreadable as well as untrue.

To me, this question is a perfect illustration of why I was
extremely dismayed when the decision to allow individual
guard tests as expressions was made.  It invited precisely
this confusion.  For example, before this decision, the
comma operator had one consistent reading: "sequence", and
the and-like effect was a simple understandable consequence
of "exception=failure" in guards.  Now we have the surprising
result that in expressions, (1 > 2, f(X)) *will* call f/1.

We are stuck with the language we have, but it would be
rather nice to have a style warning "Warning! Guard test
used as expression!  Beware of the inconsistencies!"









More information about the erlang-questions mailing list