[erlang-questions] Reading, Learning, Confused

Edwin Fine <>
Sat Jul 19 16:52:45 CEST 2008


-module(guard).
-compile([export_all]).

test(X) when X == 0; 1/X > 2 ->
    true;
test(_) ->
    false.
6> c(guard).
{ok,guard}
7> guard:test(0).
true
8> guard:test(0.4).
true
9> guard:test(0.6).
false
10>


On Sat, Jul 19, 2008 at 10:31 AM, 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?
>
> Regards,
> Alpar
>
> On Sat, 2008-07-19 at 06:50 -0700, Lev Walkin wrote:
> > Sean Allen wrote:
> > > by a small bit of example code in Programming Erlang related to guards
> > > and short circuit booleans:
> > >
> > >   f(X) when (X == 0) or (1/X > 2) ->
> > >      ...
> > >
> > > g(X) when (X == 0) orelse ( 1/X > 2) ->
> > >     ...
> > >
> > > The guard in f(X) fails when X is zero but succeeds in g(X)
> > >
> > > Can someone explain why?
> >
> >
> > Sean,
> >
> > The thing is, "or" does not short-circuit evaluation when left side
> > succeeds, whereas "orelse" does. Same short-circuit logic is
> > behind the differences between "and" and "andalso".
> >
> > Actually, the very book you read explains these differences and warns
> > about caveats a couple pages later (or earlier). Don't stop reading.
> >
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
The great enemy of the truth is very often not the lie -- deliberate,
contrived and dishonest, but the myth, persistent, persuasive, and
unrealistic. Belief in myths allows the comfort of opinion without the
discomfort of thought.
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080719/d55ee662/attachment.html>


More information about the erlang-questions mailing list