函数列表中的异常错误:foldl/3(lists.erl,第 1263 行)

Exception error in function lists:foldl/3 (lists.erl, line 1263)

问题

为什么当我 运行 一个函数可以工作,但当我将它传递给 lists:foldl 时它却不工作?


详细解释

当我 运行 终端中的特定函数时,它 returns 预期的结果。但是,如果我将此函数传递给 lists:foldl,它会抛出异常。

有问题的函数是 separate_socks/2

当我直接运行它时,它returns:

> sock_merchant:separate_socks(1, #{1 => [1]}).
#{1 => [1,1]}

> sock_merchant:separate_socks(1, #{}).        
#{1 => [1]}

但是当我 运行 test/0 时,它 returns:

> sock_merchant:test().                        
** exception error: bad function separate_socks
     in function  lists:foldl/3 (lists.erl, line 1263)

我的 sock_merchant.erl 文件包含以下内容:

% John works at a clothing store. He has a large pile of socks that he must pair
% by color for sale. Given an array of integers representing the color of each
% sock, determine how many pairs of socks with matching colors there are.

% For example, there are `n = 7` socks with colors `ar = [1,2,1,2,1,3,2]`. There
% is one pair of color 1 and one of color 2. There are three odd socks left, one
% of each color. The number of pairs is 2.

-module(sock_merchant).

-export([count_number_of_pair_of_sockets/2,
         filter_socks/2,
         separate_socks/2,
         sock_merchant/2,
         test/0,
         test_sock_merchant/0]).

%% Next: number
%% Acc: map
separate_socks(Next, Acc) ->
    KeyExists = maps:is_key(Next, Acc),

    case KeyExists of
        true ->
            CurrentKeyList = maps:get(Next, Acc),
            maps:update(Next, [Next | CurrentKeyList], Acc);
        false -> maps:put(Next, [Next], Acc)
    end.

%% Value: number
filter_socks(_, Value) ->
    if Value div 2 >= 1 -> true;
       true -> false
    end.

%% Next: {key, [number]}
%% Acc: number
count_number_of_pair_of_sockets({_, Arr}, Acc) ->
    Acc + length(Arr) div 2.

%% N: number
%% Ar: [number]
sock_merchant(_, Ar) ->
    % SocksSeparatedList = #{1: [1,1], 2: [2], ...}
    SocksSeparatedList = lists:foldl(separate_socks,
                                     #{},
                                     Ar),
    SocksWithPairs = maps:filter(filter_socks,
                                 SocksSeparatedList),
    ListOfSocksWithPairs = maps:to_list(SocksWithPairs),
    lists:foldl(count_number_of_pair_of_sockets,
                0,
                ListOfSocksWithPairs).

test_sock_merchant() ->
    3 = sock_merchant(9,
                      [10, 20, 20, 10, 10, 30, 50, 10, 20]),
    pass.

test() -> pass = test_sock_merchant().

我正在从 HackerRank 解决这个问题 https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup

术语separate_socks本身就是一个原子。这意味着当你调用 lists:foldl/3 传递 separate_socks 作为第一个参数时,你只是传递一个原子,而不是 lists:foldl/3 要求的函数:

SocksSeparatedList = lists:foldl(separate_socks,
                                 #{},
                                 Ar),

要改为传递函数 separate_socks,请将其作为函数项传递:

SocksSeparatedList = lists:foldl(fun separate_socks/2,
                                 #{},
                                 Ar),

fun关键字表示一个函数,尾部/2是函数arity(它的参数个数)。