title: Erlang using for() vs lists:foreach
body: <p>The following sample code is from Joe Armstrong's Programming Erlang book:</p>
  
  <pre><code>max(N) -&gt;
    Max = erlang:system_info(process_list),
    io:format("Maximum allowed processes:~p~n",[Max]),
    statistics(runtime),
    statistics(wall_clock),
    L = for(1, N, fun() -&gt; spawn(fun() -&gt; wait() end) end),
    {_, Time1} = statistics(runtime),
    {_, Time2} = statistics(wall_clock),
    lists:foreach(fun(Pid) -&gt; Pid ! die end, L),
    U1 = Time1 * 1000 / N,
    U2 = Time2 * 1000 / N,
    io:format("Process spawn time=~p (~p) microseconds~n",
      [U1, U2]).
  </code></pre>
  
  <p>My question is dealing with Erlang fundamentals. It looks like Joe used <code>for()</code> to spawn processes, followed by a <code>lists:foreach</code> to die them. Is there a reason to use one over the other? Why not use for() again to iterate over the the spawned process list and send them a die message? Is there an efficiency optimization here that I'm missing?</p>