Passing map type argument in function in Erlang complains error -


here's code snippet.

%% test.erl -export([count_characters/1]).  count_characters(str) ->   count_characters(str, #{}).  count_characters([h|t], #{h := n} = x) ->   count_characters(t, x#{h := n+1}); count_characters([h|t], x) ->   count_characters(t, x#{h => 1}); count_characters([], x) ->   x.  %% ershell 1> c(test). test.erl:19: illegal use of variable 'h' in map test.erl:20: illegal use of variable 'h' in map test.erl:20: variable 'n' unbound test.erl:22: illegal use of variable 'h' in map error 

i don't know why complains such error, since following code worked out fine:

%% test2.erl birthday(#{age := n} = person) ->     person#{age := n+1}.  %% ershell 1> c(test2). 2> test2:birthday(#{age => 333}). #{age => 334} 

thanks in advance.

the reason simple: map hasn't been implemented yet. take at: http://learnyousomeerlang.com/maps

also, might think of alternative implementations, using stuff that's possible maps:

count_characters(str) -> count_characters(str, #{}).  count_characters([h|t], map) ->     n = maps:get(h, map, 0),     count_characters(t, maps:put(h, n + 1, map)); count_characters([], map) -> map. 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -