首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数列表中的异常错误:foldl/3 (lists.erl,第1263行)

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

Stack Overflow用户
提问于 2020-07-12 12:15:39
回答 1查看 155关注 0票数 0

问题

为什么当我运行一个函数时,它会工作,但是当我将它传递给lists:foldl时,它就不能工作了?

详细解释

当我在终端中运行特定的函数时,它会返回预期的结果。但是,如果我将这个函数传递给list:foldl,它会抛出一个异常。

有问题的函数是separate_socks/2

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

代码语言:javascript
复制
> sock_merchant:separate_socks(1, #{1 => [1]}).
#{1 => [1,1]}

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

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

代码语言:javascript
复制
> sock_merchant:test().                        
** exception error: bad function separate_socks
     in function  lists:foldl/3 (lists.erl, line 1263)

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

代码语言:javascript
复制
% 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解决这个问题的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-12 12:29:57

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

代码语言:javascript
复制
SocksSeparatedList = lists:foldl(separate_socks,
                                 #{},
                                 Ar),

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

代码语言:javascript
复制
SocksSeparatedList = lists:foldl(fun separate_socks/2,
                                 #{},
                                 Ar),

fun关键字表示一个函数,尾随的/2是函数性(其参数数)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62860961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档