[erlang-questions] Reading, Learning, Confused
Alpár Jüttner
alpar@REDACTED
Sun Jul 20 20:52:22 CEST 2008
On Sun, 2008-07-20 at 14:53 -0300, Toby Thain wrote:
> On 19-Jul-08, at 11: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?
>
>
> I also did a double take on this text, but my reading of "always
> short-circuited" is "it is always safe to short circuit [since...]",
> so a (normally) non-short-circuit operator can always be short-
> circuited.
Do you mean that 'or' can always be replaced by 'orelse'? We've learnt
that it is not true.
> (Compare, e.g. C's | and ||, where | may be used deliberately for
> side-effects on the RHS.)
It is a different story. In C, '|' is the bitwise or operation, it
corresponds to 'bor' in Erlang. ('||' in C is the same as 'orelse' and
there is no C equivalent for the 'or' operator of Erlang.)
Regards,
Alpar
>
> --Toby
>
> >
> > 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
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list