[erlang-questions] Question about order of matching

Dale Harvey <>
Wed Mar 4 02:31:30 CET 2009


getResults(N) ->
    receive
       {Test, ET, N, Bytes, Pid} ->

N is already bound and the receive clause
wont match unless its equal

2009/3/4 Allen McPherson <>

> I have the following receive loop:
>
> getResults(0) ->
>     'getResults ok';
> getResults(N) ->
>     receive
>        {Test, ET, N, Bytes, Pid} ->
>            io:format("~p: ET: ~p~n", [Test, ET]),
>            getResults(N-1);
>        'unknown test' ->
>            io:format("unknown test~n"),
>            getResults(N-1);
>        Any ->
>            io:format("~p~n", [Any]),
>            getResults(N-1)
>     end.
>
>
> When it is sent the following message:
>
> MasterPid ! {allpairs, timer(wall_clock), N, Bytes, self()},
>
> ..the Any clause (which I put in for debugging purposes because nothing
> was matching and the receive was blocking) matches and prints the
> following:
>
> {allpairs,2346,100,1048576,<6083.48.0>}
> {allpairs,2109,100,1048576,<6084.38.0>}
> {allpairs,2152,100,1048576,<6041.48.0>}
> {allpairs,2159,100,1048576,<6082.48.0>}
>
> I would have thought the first clause should have matched?
> I tested in the shell:
>
> ()1> {Test, ET, N, Bytes, Pid} = {allpairs,
> 2346,100,1048576,<6083.48.0>}.
>  1: syntax error before: '<'
>
> The shell does not like the Pid syntax and I've Googled around to see
> why this
> is a problem even when using pid_to_list and list_to_pid.
> This works though:
>
> ()1> {Test, ET, N, Bytes, Pid} = {allpairs,
> 2346,100,1048576,self()}.
> {allpairs,2346,100,1048576,<0.37.0>}
>
> And, removing Pid in shell works too:
>
> ()1> {Test, ET, N, Bytes} = {allpairs,2346,100,1048576}.
> {allpairs,2346,100,1048576}
>
> The same tactic (removing Pid) in the code still matches the Any clause.
>
> Perhaps I'm not understanding something fundamental here?  I suspect
> this is
> a dumb mistake on my part but I can't see it.
>
> --
> Al
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090304/975bf58f/attachment.html>


More information about the erlang-questions mailing list