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

时间:2020-07-12 12:15:39

标签: erlang

问题

为什么当我运行一个函数时它起作用,但是当我将其传递给lists:foldl时却不起作用?


详细说明

当我在终端中运行特定功能时,它将返回预期结果。但是,如果我将此函数传递给list:foldl,它将引发异常。

有问题的功能是separate_socks/2

当我直接运行它时,它返回:

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

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

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

> 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

解决此问题

1 个答案:

答案 0 :(得分:1)

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(其参数个数)。

相关问题