[erlang-questions] Apparently strange dict behaviour

Raimo Niskanen raimo+erlang-questions@REDACTED
Tue Aug 9 09:39:14 CEST 2011


Hi.

I am sorry I did not have time to read your post thoroughly,
because I think you just ran into the feature of the shell
(io:format("~p", ...)) being "intelligent" and prints lists
of integers as strings if the integers can represent printable
characters.

There is no character type in Erlang, integers are used instead,
and lists of integers instead of strings. (or binaries instead
of strings but that is another story)

dict:append/3 I think creates a list of the values, your values
are integers, hence you get something that is mistaken for a string.

But as I said I did not read thoroughly...

/ Raimo



On Tue, Aug 09, 2011 at 04:28:38PM +0930, Darryn wrote:
> 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) ->
>     receive
>         {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)
>     end.
> 
> samples(Index, N) ->
>     random:seed(erlang:now()),
>     {Index, do_samples(0, N, 0)}.
> 
> doit() ->
>     Results = pmap([{1,100}, {2,100}, {3, 100}], fun samples/2),
>     dict:to_list(Results).
> 
> 
> 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).    
> {ok,pisim}
> 21> pisim:doit().
> Collected result:{1,88}
>     ->is_integer?true
>     ->[{1,"X"}]
> Worker <0.114.0> terminated with normal
> Collected result:{1,87}
>     ->is_integer?true
>     ->[{1,"XW"}]
> Worker <0.116.0> terminated with normal
> Collected result:{2,76}
>     ->is_integer?true
>     ->[{2,"L"},{1,"XW"}]
> Worker <0.115.0> terminated with normal
> [{2,"L"},{1,"XW"}]
> 22> c(pisim).    
> {ok,pisim}
> 23> pisim:doit().
> Worker <0.118.0> terminated with normal
> Collected result:{1,78}
>     ->is_integer?true
>     ->[{1,"N"}]
> Worker <0.123.0> terminated with normal
> Collected result:{2,83}
>     ->is_integer?true
>     ->[{2,"S"},{1,"N"}]
> Worker <0.124.0> terminated with normal
> [{2,"S"},{1,"N"}]
>  
> 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!
> 
> Darryn.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list