[erlang-questions] difference between ", " and "andalso" in guards

Bob Ippolito bob@REDACTED
Tue May 20 00:48:36 CEST 2014


It's not really a performance optimization, it's a difference in semantics.

This function in pseudo-erlang:

f(X) when A(X); B(X) -> Y(X);
f(X) -> Z(X).

Is operationally equivalent to this one:

f(X) when A(X) -> Y(X);
f(X) when B(X) -> Y(X);
f(X) -> Z(X).

Using orelse behaves differently, it doesn't treat the arguments quite so
independently.

1> (fun () when hd([]) orelse true -> ok end)().
** exception error: no function clause matching
erl_eval:'-inside-an-interpreted-fun-'()
2> (fun () when hd([]); true -> ok end)().
ok



On Mon, May 19, 2014 at 3:05 PM, Steve Davis <steven.charles.davis@REDACTED
> wrote:

> I can't imagine an output difference. Shouldn't "," =:= "andalso" and ";"
> =:= "orelse".
>
> The difference appears to be a performance optimization - where would the
> "orelse shortcut" be incorrect?
>
> (Could be I'm underthinking this)...
>
>
> On Saturday, May 17, 2014 11:20:08 PM UTC-5, Adel Zhang wrote:
>>
>> hi, I am currently reading "learnyousomeerlang". In "Syntax in Functions"
>>
>> chapter "guards" section, Fred said
>>
>>
>>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140519/77a99994/attachment.htm>


More information about the erlang-questions mailing list