[erlang-questions] Reading, Learning, Confused

Sean Allen <>
Sat Jul 19 16:21:08 CEST 2008


On Jul 19, 2008, at 9:50 AM, 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.

Actually its about 49 pages later where short circuit booleans are  
discussed
and 37 pages for boolean expressions. *).

How is ' f(X) when (X == 0) or (1/X > 2)' and or if it fails for 0?
What is the difference at that point between and/or. I cant find  
anything really
detailed on that.

Is or equivalent to , and orelse equiv to ;?

That is the only way this makes any sense to me. Except that well

--

page 94... boolean expressions:

3> true or false
true
4> (2 > 1 ) or ( 3 > 4 )
true

makes sense.

still cant wrap my head around given the above... why....

f(X) when (X == 0) or (1/X > 2)

fails.

does it fail because it would be a divide by zero?

if yes, why does this also fail with divide by zero?

(X == 0) orelse (1/X > 2)

and why wouldnt the g(X) guard fail?

and lord so confused... why are both of these true then:

1> X=1.
1
2> ( X == 1 ) or ( 1/X > 2 ).
true
3> ( X == 1 ) orelse ( 1/X > 2 ).
true

the above... that makes sense to me. those guards...
totally lost. TOTALLY.





More information about the erlang-questions mailing list