<div dir="ltr">You are getting twisted in knots.<br><br>1> X =0.<br>0<br>2> (X == 0) orelse (1/X > 2).<br>true<br>3> f(). <br>ok<br>4> X = 1.                    <br>1<br>5> (X == 0) orelse (1/X > 2).<br>
false<br>8> f().<br>9> X = 0. <br>0<br>10> (X == 0) or (1/X > 2).<br>** exception error: bad argument in an arithmetic expression<br>     in operator  '/'/2<br>        called as 1 / 0<br><br>So the orelse construct works as advertised. It will not evaluate any expressions after the first false condition and prevents divide by zero.<br>
<br>-module(guard).<br>-compile([export_all]).<br><br>test(X) when (X == 0) orelse (1/X > 2) -><br>    true;<br>test(_) -><br>    false.<br>1> c(guard).<br>{ok,guard}<br>2> guard:test(0). <br>true<br>3> guard:test(0.0).<br>
true<br>4> guard:test(0.5).<br>false<br>5> guard:test(0.4).<br>true<br><br>Hope this helps.<br><br><br><div class="gmail_quote">On Sat, Jul 19, 2008 at 10:21 AM, Sean Allen <<a href="mailto:sean@monkeysnatchbanana.com">sean@monkeysnatchbanana.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d"><br>
On Jul 19, 2008, at 9:50 AM, Lev Walkin wrote:<br>
<br>
> Sean Allen wrote:<br>
>> by a small bit of example code in Programming Erlang related to<br>
>> guards  and short circuit booleans:<br>
>>  f(X) when (X == 0) or (1/X > 2) -><br>
>>     ...<br>
>> g(X) when (X == 0) orelse ( 1/X > 2) -><br>
>>    ...<br>
>> The guard in f(X) fails when X is zero but succeeds in g(X)<br>
>> Can someone explain why?<br>
><br>
><br>
> Sean,<br>
><br>
> The thing is, "or" does not short-circuit evaluation when left side<br>
> succeeds, whereas "orelse" does. Same short-circuit logic is<br>
> behind the differences between "and" and "andalso".<br>
><br>
> Actually, the very book you read explains these differences and warns<br>
> about caveats a couple pages later (or earlier). Don't stop reading.<br>
<br>
</div>Actually its about 49 pages later where short circuit booleans are<br>
discussed<br>
and 37 pages for boolean expressions. *).<br>
<br>
How is ' f(X) when (X == 0) or (1/X > 2)' and or if it fails for 0?<br>
What is the difference at that point between and/or. I cant find<br>
anything really<br>
detailed on that.<br>
<br>
Is or equivalent to , and orelse equiv to ;?<br>
<br>
That is the only way this makes any sense to me. Except that well<br>
<br>
--<br>
<br>
page 94... boolean expressions:<br>
<br>
3> true or false<br>
true<br>
4> (2 > 1 ) or ( 3 > 4 )<br>
true<br>
<br>
makes sense.<br>
<br>
still cant wrap my head around given the above... why....<br>
<div class="Ih2E3d"><br>
f(X) when (X == 0) or (1/X > 2)<br>
<br>
</div>fails.<br>
<br>
does it fail because it would be a divide by zero?<br>
<br>
if yes, why does this also fail with divide by zero?<br>
<div class="Ih2E3d"><br>
(X == 0) orelse (1/X > 2)<br>
<br>
</div>and why wouldnt the g(X) guard fail?<br>
<br>
and lord so confused... why are both of these true then:<br>
<br>
1> X=1.<br>
1<br>
2> ( X == 1 ) or ( 1/X > 2 ).<br>
true<br>
3> ( X == 1 ) orelse ( 1/X > 2 ).<br>
true<br>
<br>
the above... that makes sense to me. those guards...<br>
totally lost. TOTALLY.<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>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.<br>
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
</div>