Edwin Fine <>
Sat Jul 19 16:47:04 CEST 2008

```You are getting twisted in knots.

1> X =0.
0
2> (X == 0) orelse (1/X > 2).
true
3> f().
ok
4> X = 1.
1
5> (X == 0) orelse (1/X > 2).
false
8> f().
9> X = 0.
0
10> (X == 0) or (1/X > 2).
** exception error: bad argument in an arithmetic expression
in operator  '/'/2
called as 1 / 0

So the orelse construct works as advertised. It will not evaluate any
expressions after the first false condition and prevents divide by zero.

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

test(X) when (X == 0) orelse (1/X > 2) ->
true;
test(_) ->
false.
1> c(guard).
{ok,guard}
2> guard:test(0).
true
3> guard:test(0.0).
true
4> guard:test(0.5).
false
5> guard:test(0.4).
true

Hope this helps.

On Sat, Jul 19, 2008 at 10:21 AM, Sean Allen <>
wrote:

>
> On Jul 19, 2008, at 9:50 AM, 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.
>
> Actually its about 49 pages later where short circuit booleans are
> discussed
> and 37 pages for boolean expressions. *).
>
> How is ' f(X) when (X == 0) or (1/X > 2)' and or if it fails for 0?
> What is the difference at that point between and/or. I cant find
> anything really
> detailed on that.
>
> Is or equivalent to , and orelse equiv to ;?
>
> That is the only way this makes any sense to me. Except that well
>
> --
>
> page 94... boolean expressions:
>
> 3> true or false
> true
> 4> (2 > 1 ) or ( 3 > 4 )
> true
>
> makes sense.
>
> still cant wrap my head around given the above... why....
>
> f(X) when (X == 0) or (1/X > 2)
>
> fails.
>
> does it fail because it would be a divide by zero?
>
> if yes, why does this also fail with divide by zero?
>
> (X == 0) orelse (1/X > 2)
>
> and why wouldnt the g(X) guard fail?
>
> and lord so confused... why are both of these true then:
>
> 1> X=1.
> 1
> 2> ( X == 1 ) or ( 1/X > 2 ).
> true
> 3> ( X == 1 ) orelse ( 1/X > 2 ).
> true
>
> the above... that makes sense to me. those guards...
> totally lost. TOTALLY.
>
>
> _______________________________________________
> 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/8ec90eba/attachment.html>
```