[erlang-questions] Reading, Learning, Confused

Edwin Fine <>
Sat Jul 19 19:40:40 CEST 2008


Darren,

I am not sure I understand what is "not quite right". All I can see is that
I didn't mention that a failed (as in, there was an exception or error)
guard condition is treated as false.

I think your code works exactly as I would have expected. I see the sequence
as:

First Clause:
f(0) when (0 == 0) [true] or ((1/X) > 2) [error = false] => entire guard
fails, so try next clause.
Next Clause:
f(0) when (0 == 0) true => short-circuit all subsequent guard conditions, so
guard is true and clause is executed.

I was saying that the absence of side-effects in guard conditions allows
Erlang to short-circuit  guard expressions of the format

   when cond1; cond2; cond3

or

   when cond1, cond2, cond3

This was not referring to use of or/orelse/and/andalso, which I tend to
avoid in guards.

Please help me understand what you meant in your post.

On Sat, Jul 19, 2008 at 12:59 PM, Darren New <> wrote:

> Edwin Fine wrote:
> > of guards, it is guaranteed that leaving out the evaluation of one or
> > more guards will not change the state of the program.
>
> I don't think that's quite right.
>
> f(X) when (X == 0) or ((1 / X) > 2) ->
>    "does not return for zero X";
> f(X) when (X == 0) orelse ((1 / X) > 2) ->
>   "does return for zero X".
>
> I might be misunderstanding here, but I understand that an "abrupt
> return" from a calculation in a guard is treated the same as "false".
>
> So in the first case, when X is 0, the guard evaluates to false, because
> 1/X errors out, making the entire expression false.
>
> In the second case, 1/X isn't evaluated when X==0, making the entire
> expression true.
>
> It does seem like the documentation is wrong. I just tried the above
> fragment and it printed "does return" as its answer.
>
> --
> Darren New / San Diego, CA, USA (PST)
>  Helpful housekeeping hints:
>   Check your feather pillows for holes
>    before putting them in the washing machine.
> _______________________________________________
> 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/9208ce3c/attachment.html>


More information about the erlang-questions mailing list