[erlang-questions] Parallel Shootout & a style question

Ulf Wiger (TN/EAB) <>
Tue Sep 2 09:49:44 CEST 2008


Mats Cronqvist skrev:
> 
>   i'm partial to the monitor-exit idiom.
 >   gets rid of the Self for one thing.
> 
>   mats
> 
>  depthLoop(D,M) when D > M -> ok;
>  depthLoop(D,M) ->
>    erlang:spawn_monitor(fun()-> slave(D, M) end),
>    depthLoop (D+2,M),
>    receive {'DOWN',_,_,_,done} -> done end.
> 
>  slave(D, M) ->
>    N = 1 bsl (M-D + ?Min),
>    io:fwrite("~w\t trees of depth ~w\t check: ~w~n",
>          [ 2*N, D, sumLoop(N,D,0) ]),
>    exit(done).

Of course, as pioneered by Robert Virding, we can still
catch the results this way, if we want to, and we
probably do want to, to make sure that printouts appear
in the expected order:


The slave does exit(Result), and the collector does
receive {'DOWN',_,_,_,Result}.

Since Robert used this trick in the shell REPL code,
and he was one of the inventors of the language, it
has to be Good Style.  (:

BR,
Ulf W



More information about the erlang-questions mailing list