[erlang-questions] Parallel Shootout & a style question

Mats Cronqvist <>
Tue Sep 2 10:05:16 CEST 2008


Ulf Wiger (TN/EAB) wrote:
> 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}.

  being an Aspiring Architecture Astronaut i didn't actually look at the 
problem spec...
  i guess you mean that the main/1 function should be rewritten as a 
collector?

> 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.  (: 
 "trick"? how disparaging :>
  i thought the REPL was older than monitor? it uses 
trap_exit/link/catch? that, otoh, is pretty bad.

  mats



More information about the erlang-questions mailing list