[erlang-questions] Question about order of matching

Allen McPherson <>
Wed Mar 4 17:25:55 CET 2009


Ouch. That "silent" failure would have taken me a bunch
of time to find. Add that to my slowly growing bag of
Erlang experience. Next time I will try and test the
ENTIRE code in the shell--simplified little tests like
the one I did obviously don't cut it for problems like
this.



harveyd wrote:
> 
> 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
>>
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 

-- 
View this message in context: http://www.nabble.com/Question-about-order-of-matching-tp22321837p22333467.html
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list