<div dir="ltr">I interpret those words as follows:<br><br>As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are allowed in guards. Please note that evaluation is always short-circuited in guards.<br>This is because guard tests are known to be free of side effects. If a guard condition is free of side-effects, that means that in a sequence of guards, it is guaranteed that leaving out the evaluation of one or more guards will not change the state of the program. For example, let's say that user-defined functions were allowed in guards.<br>
<br>f(X) -> put(x, X), true.<br>g() -> put(x, true), put(y,"g() was called"), true.<br>h() -> get(x).<br>test(X) when f(X), g(), h() -> ok. % Will not compile - illegal Erlang<br><br>What will get(x) and get(y) return after test(X) is run? Will it make a difference to the result if guards are not short-circuited?<br>
<br>Non-short-circuited guards:<br>test(true): f(true) sets x to true, g() sets x to true, y to the string. End result: x is true, y is "g() was called".<br>test(false): f(false) sets x to false, g() sets x to true. End result: x is true, y is "g() was called".<br>
<br>Short-circuited guards with side-effects:<br>test(true): f(true) sets x to true, g() sets x to true, y to the string. End result: x is true, y is "g() was called".<br>
test(false): f(false) sets x to false, g() is not evaluated. End result: x is false, y is undefined.<br><br>So skipping a guard test if it has a side effect can change the state of the whole program, which would make short-circuit evaluation impossible. But because no guard tests can have side-effects, the state of the program cannot be changed if one or more guard tests are not evaluated.<br>
<br>Hope this makes sense.<br><br><br><br><div class="gmail_quote">On Sat, Jul 19, 2008 at 11:22 AM, Alpár Jüttner <<a href="mailto:alpar@cs.elte.hu">alpar@cs.elte.hu</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">> As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are<br>
> allowed in guards. In guards, however, evaluation is always<br>
> short-circuited since guard tests are known to be free of side<br>
> effects.<br>
> (Section 6.14, Short-Circuit Boolean Expressions)<br>
><br>
> Something is wrong here, isn;t it?<br>
<br>
</div>I mean<br>
<br>
* What does the word "however" mean here? Does it mean that if<br>
they are not in a guard, orelse/andelse might be non<br>
short-circuited?<br>
* How does the freedom from side effects are related to the<br>
short-circuited evaluation?<br>
<br>
Regards,<br>
Alpar<br>
<div><div></div><div class="Wj3C7c"><br>
><br>
> Regards,<br>
> Alpar<br>
><br>
> On Sat, 2008-07-19 at 06:50 -0700, Lev Walkin wrote:<br>
> > Sean Allen wrote:<br>
> > > by a small bit of example code in Programming Erlang related to guards<br>
> > > and short circuit booleans:<br>
> > ><br>
> > > f(X) when (X == 0) or (1/X > 2) -><br>
> > > ...<br>
> > ><br>
> > > g(X) when (X == 0) orelse ( 1/X > 2) -><br>
> > > ...<br>
> > ><br>
> > > The guard in f(X) fails when X is zero but succeeds in g(X)<br>
> > ><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>
><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>
_______________________________________________<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></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>