How to accumulate results with SSA in Erlang?
10:35 19 Jul 2013

This is a total functional newbie question.

I'm trying to learn some Erlang and have created a (hopefully concurrent) Monte Carlo Simulation where multiple processes are spawned, which report their local results to the parent process via message passing.

So in the parent process I have something like

parent(NumIterations, NumProcs) ->
    random:seed(),

    % spawn NumProcs processes
    lists:foreach(
        spawn(moduleName, workerFunction, [self(), NumIterations/NumProcs, 0, 0]),
        lists:seq(0, NumProcs - 1)),

    % accumulate results
    receive
        {N, M} -> ???; % how to accumulate this into global results?
        _      -> io:format("error")
    end.

Let's say I want to sum up all Ns and Ms received from the spawned processes.

I understand that accumulating values is usually done via recursion in functional programming, but how to do that within a receive statement..?

functional-programming erlang