[erlang-questions] Another question on Maps pattern matching

Santosh Kumar <>
Wed Dec 10 03:00:01 CET 2014


Thanks, Bjorn. Kind of a bummer -- but I'm having so much fun with the
language to let these come in the way :).

Best,
Santosh

On Tue, Dec 9, 2014 at 8:57 PM, Björn-Egil Dahlberg <
> wrote:

> 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/20141209/6a9c6e8b/attachment.html>


More information about the erlang-questions mailing list