[erlang-questions] Another question on Maps pattern matching

Björn-Egil Dahlberg <>
Wed Dec 10 02:57:13 CET 2014


Ah, when the book appears before the implementation .. or even the spec.

Only literal keys are allowed in Maps in the current release (17.0+). And
with literals we mean no variables in this case.

In the master branch (18) we currently allow for variables but not as
described in joes book (at least not yet). Variables has to be bound before
they are used there.

// Björn-Egil

2014-12-10 2:46 GMT+01:00 Santosh Kumar <>:

> 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
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141210/c8f123de/attachment.html>


More information about the erlang-questions mailing list