My question is related to an existing question (and boy, was I surprised to see it was a known bug in Erlang itself!). I'm using the same count_characters
example from the book, and getting the same error even though I'm on R19.
Code:
% Frequency count of characters in a string
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.
and the error:
1> c(lib_misc).
lib_misc.erl:40: illegal pattern
lib_misc.erl:41: variable 'N' is unbound
error
Here line 40 refers to the first clause of count-characters/2
.
My questions are:
- I'm not able to understand what exactly the bug is from the linked SO question. Can someone please describe in simple terms which variable is causing the error and why?
- Is this still not fixed in R19?? If not, when will it be? It's sad to see the author's book providing an example that is so badly broken.
I can see the accepted answer on the linked page uses stuff like maps:update
. I could do the same, but I'd first like to know why the error exists.