[erlang-questions] count_characters example from Programming Erlang

Mark Bucciarelli mkbucc@REDACTED
Fri Feb 5 02:59:47 CET 2016


Hi,

I'm trying to compile the count_characters example from Programming Erlang
book.
But I get an error, and I can't see a typo.

-module(t).
-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.

1> c(t).
t.erl:7: variable 'H' is unbound
error
2>

This behavior contradicts the point made in the text:

        There are two things to note about count_characters/2.
        In the first clause, the variable H inside the map is also
        defined outside the map and thus is bound (as required).
        In the second clause, we use map_extend to add a new
        key to the map.

Thanks!

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160204/ddac9c17/attachment.htm>


More information about the erlang-questions mailing list