illegal pattern in map of Erlang

2019-01-18 10:02发布

The code is below:

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

when compiling the code in the Erlang shell, it reported the following errors:

1> c(map_demo).
map_demo.erl:7: illegal pattern
map_demo.erl:8: variable 'N' is unbound
map_demo.erl:10: illegal use of variable 'H' in map
map_demo.erl:7: Warning: variable 'H' is unused
error

I'm new in Erlang, and just can't find anything wrong by myself. How to correct it?

标签: map erlang
7条回答
看我几分像从前
2楼-- · 2019-01-18 10:29

I guess you are using R17 since this feature is available only from this version.

looking at some doc, my understanding is that you should write the code that way (I can't test it, I am still using R15 :o)

-module(map_demo).
-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.
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-18 10:31

The answers from IRC (#erlang@freenode):

  1. variables as keys in matches are not supported yet (release 17.0)
  2. A more general issue affects matching arguments of a function: line 7's H is matched 2 times; or once and used to match N then. (This issue also appears with binaries)

This should be solved in the coming releases.

As of release 17 this works:

-module(count_chars).
-export([count_characters/1]).

count_characters(Str) ->
        count_characters(Str, #{}).

%% maps module functions cannot be used as guards (release 17)
%% or you'll get "illegal guard expression" error
count_characters([H|T], X) ->
    case maps:is_key(H,X) of
        false -> count_characters(T, maps:put(H,1,X));
        true  -> Count = maps:get(H,X),
                         count_characters(T, maps:update(H,Count+1,X))
    end;
count_characters([], X) ->
        X.

Here is another version (only tested on 18) that is slightly more similar to the one in the book:

-module(count_chars).
-export([count_characters/1]).

count_characters(Str) ->
        count_characters(Str, #{}).

count_characters([H|T], X) ->
    case maps:is_key(H,X) of
        false -> count_characters(T, X#{ H => 1 });
        true  -> #{ H := Count } = X,
                 count_characters(T, X#{ H := Count+1 })
    end;
count_characters([], X) ->
        X.
查看更多
男人必须洒脱
4楼-- · 2019-01-18 10:36

Quoted from OTP 17.0 Release Notes:

OTP-11616 == erts stdlib hipe dialyzer compiler typer ==

    EEP43: New data type - Maps

    With Maps you may for instance:

    -- M0 = #{ a => 1, b => 2}, % create associations

    -- M1 = M0#{ a := 10 }, % update values

    -- M2 = M1#{ "hi" => "hello"}, % add new associations

    -- #{ "hi" := V1, a := V2, b := V3} = M2. % match keys with
    values

    For information on how to use Maps please see the Reference
    Manual.

    The current implementation is without the following features:

    -- No variable keys

    -- No single value access

    -- No map comprehensions

    Note that Maps is experimental during OTP 17.0.

Currently you can use maps module to implement count_characters:

count_characters(Str) ->
    count_characters(Str, #{}).

count_characters([H|T], X) -> 
    count_characters(T, maps:put(H, maps:get(H, X, 0) + 1, X));
count_characters([], X) ->
    X.
查看更多
We Are One
5楼-- · 2019-01-18 10:38

Problem in match syntax.

Fof match use :=. Example

test(#{ key := Test }) -> Test.

And for associated key and value use =>. Example: M = #{ keynew => 123 }

查看更多
趁早两清
6楼-- · 2019-01-18 10:44

When you want to match a map, you need like this:

#{key1 := Pattern1, key2 := Pattern2, ...} = VarContainingAMap.

you can read the ducoment: http://joearms.github.io/2014/02/01/big-changes-to-erlang.html

查看更多
Deceive 欺骗
7楼-- · 2019-01-18 10:47

@EWit, Felipe Mafra:

maps does just what it is supposed to do; what's missing here is the reduce part:

count(Str) -> M = count_chars(Str, maps:new()), % maps part, bad naming
    L = maps:to_list(M),                        % to be able to sum
    N = [X || {_,X} <- L],                      % strip the numbers
    lists:sum(N).                               % sum them up

count_chars([H|T], Map) when is_map(Map)->
    N = maps:get(H, Map, 0),
    count_chars(T, maps:put(H, N + 1, Map));
count_chars([], Map) -> Map.
查看更多
登录 后发表回答