[erlang-questions] Reading, Learning, Confused

Lev Walkin vlm@REDACTED
Sat Jul 19 15:50:06 CEST 2008


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.

-- 
vlm



More information about the erlang-questions mailing list