[erlang-questions] Apparently strange dict behaviour

Darryn djreid@REDACTED
Tue Aug 9 08:58:38 CEST 2011

Kind Sirs/Madams,

I'm new to erlang, but have extensive prior experience with Haskell. I thought 
I'd play with a small version of a parallel map/fold operation, but have run 
into an annoying problem. The test example is just a parallel estimation by 
sampling of Pi.

pmap(Input, MapFn) ->
    process_flag(trap_exit, true),
    spawn_workers(self(), MapFn, Input),
    M = length(Input),
    collect(M, dict:new()).

spawn_workers(Pid, Fn, Pairs) ->
    lists:foreach(fun({K1, V1}) ->
        spawn_link(fun() -> worker(Pid, Fn, {K1, V1}) end) end, Pairs).

worker(Pid, Fn, {K1, V1}) ->
    Pid ! Fn(K1, V1).

collect(0, Dict) -> Dict;
collect(N, Dict) ->
        {Key2, Val2} ->
            DictD = dict:append(Key2, Val2, Dict),                                    
%% <- problem: inserted Val2 integer apparently turns into a string?!!
            io:format("Collected result:{~p,~p}~n", [Key2, Val2]),
            io:format("    ->is_integer?~p~n", [is_integer(Val2)]),
            io:format("    ->~p~n", [dict:to_list(DictD)]),
            collect(N, DictD);
        {'EXIT', Who, Why} ->
            io:format("Worker ~p terminated with ~p~n",[Who, Why]),
            collect(N-1, Dict)

samples(Index, N) ->
    {Index, do_samples(0, N, 0)}.

doit() ->
    Results = pmap([{1,100}, {2,100}, {3, 100}], fun samples/2),

The problem seems to lie in the collect function, where the collected Val2 
integer (the io:format confirms it is an integer) suddenly becomes a string 
upon insertion into the dictionary. Here is example output to illustrate.

20> c(pisim).    
21> pisim:doit().
Collected result:{1,88}
Worker <0.114.0> terminated with normal
Collected result:{1,87}
Worker <0.116.0> terminated with normal
Collected result:{2,76}
Worker <0.115.0> terminated with normal
22> c(pisim).    
23> pisim:doit().
Worker <0.118.0> terminated with normal
Collected result:{1,78}
Worker <0.123.0> terminated with normal
Collected result:{2,83}
Worker <0.124.0> terminated with normal
I don't understand how the Val2 integer suddenly turns into a string upon 
being inserted into the dictionary; when I manually cut and paste the relevant 
bits of code into the shell I get dictionary values with integers as I intend.

I wonder if anyone more experienced can see what I've done wrong? Thanks so 
much for any help!


More information about the erlang-questions mailing list