[erlang-questions] Another question on Maps pattern matching

Santosh Kumar <>
Wed Dec 10 02:46:14 CET 2014


Hi,
Working my way through Joe's book and I have another issue with getting one
of his code snippets to compile. Specifically, pattern matching within the
args list of a function definition just isn't working. Here's the code:

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.

It's not liking the variable binding of 'H'. So if I do something like so,
it's not "evaluating" the bound value in a map.

39> Henry8.

#{born => 1491,class => king,died => 1547}

42> B = born.

born

43> #{B := HB} = Henry8.

* 1: illegal use of variable 'B' in map


Any help is much appreciated.


Thanks

Santosh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141209/907c85ad/attachment.html>


More information about the erlang-questions mailing list