[erlang-questions] Reading, Learning, Confused

Robert Virding rvirding@REDACTED
Mon Jul 21 23:26:30 CEST 2008

The boolean operators and, or, xor and not are the originals and evaluate
all their arguments, they have never been short circuiting, and are like the
bitwise C operators. Orelse and andalso came much later.

2008/7/21 Thomas Lindgren <thomasl_erlang@REDACTED>:

> I normally agree with those recommendations, but with a couple of caveats:
> 1. You can't nest ';', but you can nest orelse. This can be useful for
> macros and suchlike.
> 2. I seem to recall that ';' is implemented by duplicating the clauses,
> while andalso/orelse is not. If so, andalso/orelse _may_ yield somewhat more
> compact code.
> Both of these properties are basically accidents of history (ie, hacks that
> have been cast in stone).

Yes, you have to go back in history to see why guards are what they are.

Guards were originally meant to be just simple tests which were considered
part of the pattern matching process but were clearer and simpler to write
outside the actual pattern. They came directly from the guards occurring in
the flat concurrent logic languages.

Originally guards were a sequence of simple tests, a guard sequence, which
could either succeed or fail. An exception in a guard test was made to be
equivalent to failure of the test to make the guards simpler and shorter.
With these original guards the order of evaluation *was*  irrelevant but the
compiler did them left to right.

    when test1, test2, ... ->

Then their came a request to have alternate guards for the same head and
body. To do this guards were extended to be a set of alternate guard
sequences separated by ';'. The semantics were the same within each guard
sequence. The sequences were evaluated from left to right, if a sequence
failed then the next one was tried. An exception was equivalent to failure
as before but the only led to that sequence failing, not the whole guard.
This was consistent with other uses of ';' as separator.

    when guard_seq1 ; guard_seq2 ; ... -> ..

Guard tests were still quite different semantically from "normal"
expressions and it was not too different to *see* the difference.

Finally guards were extended to be full boolean expressions, more or less.
While this made them more useful in some ways it also made them much more
difficult and confusing. As well as making them look more like "normal"
expressions which they still aren't.

 There is no problem with code duplication when using ';'.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080721/f9aed17b/attachment.htm>

More information about the erlang-questions mailing list