[erlang-questions] an erlang "gotcha"

Sébastien Saint-Sevin <>
Thu Apr 5 13:11:27 CEST 2007


> 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 /= [] ->

For what you want, it should be :

	is_pid(Pid) and (L /= []) ->

See operator precedence in the reference manual.


> 	    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.
> 

Cheers,

Sebastien.



More information about the erlang-questions mailing list