[erlang-questions] an erlang "gotcha"

Peter Lund erlang@REDACTED
Thu Apr 5 13:18:16 CEST 2007


In your example 'and' is evaluated before '/=', so it tries to do 
'is_pid(Pid) and L' first which fails since L is not boolean. It seems 
to go against normal intuition, that changing 'and' to 'andalso' makes 
it do '/=' before 'andalso'. Anyhow this is explained by the Operator 
Precedence list:

http://erlang.org/doc/doc-5.5.4/doc/reference_manual/expressions.html#6.24

/Peter

Anthony Shipman skrev:
> Is there a place where difficulties with erlang and their solutions are 
> described? Perhaps another page at Trapexit.
>
> Here's one that got me.
>
> -module(g).
> -export([run/0]).
>
> run() ->
>     Pid = spawn(fun() -> io:fwrite("spawned~n") end),
>     L = [1, 2, 3],
>     io:fwrite("Pid=~p L=~p~n", [Pid, L]),
>     if
> 	is_pid(Pid) and L /= [] ->
> 	    io:fwrite("it worked~n");
>
> 	true ->
> 	    io:fwrite("it failed~n")
>     end.
>
> It prints out "it failed". If you change the 'and' to 'andalso' it works.
>
>   




More information about the erlang-questions mailing list