[erlang-questions] A tad confused about function parameters

Allen McPherson mcpherson@REDACTED
Wed Mar 11 23:31:55 CET 2009


Here's a snippet of code that I'm using:

%%  Once nodes have been started we launch the code to execute the
%%  benchmarks.
launch(L) ->
     launch(L, []).
launch([], Pids) ->
     Pids;
launch([H|T], Pids) ->
     S = self(),             %  loop(self(), start) would evaluate  
self() inside loop() function
     launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S, start)  
end)])).

%%  Main benchmark loop.  Receives request for particular type of test.
%%  Spawns code to handle benchmark on its local node.
loop(MasterPid, Mode) ->
     ...


The line in question is:
            S = self(),
----->     launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S,  
start) end)])).

I find that if I use:

            launch(T, lists:append(Pids, [spawn(H, fun() ->  
loop(self(), start) end)])).

instead, self() is evaluated everywhere in loop() that MasterPid occurs.
Intuitively I sort of get it, by formally I don't really understand
what's going on here...why the delayed evaluation? I would think it  
would be
evaluate before the spawn, but it's getting evaluated after the spawn  
on node
'H'.

Probably another dumb question, but can someone shed some light this.
It seems like a fundamental concept to understand.

--
Al



More information about the erlang-questions mailing list