Erlang,查找列表中数字出现的次数 [英] Erlang, finding the number of occurrences of a number in a list

查看:119
本文介绍了Erlang,查找列表中数字出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Erlang的新手,正在尝试编写一个程序,该程序将像[1,5,4,5,3,2,2,2,8,11]这样的数字作为函数的输入参数.该函数应返回一个元组列表,其中记录了该数字及其在列表中的出现次数,如下所示.

I am new to Erlang and trying to write a program that will take a list of numbers like this [1,5,4,5,3,2,2,8,11] as an input parameter of a function. The function should return a list of tuples that records the number and it's number of appearances in the list like so.

[{1,1},{2,2},{3,1},{4,1},{5,1},{8,1},{11,1}].

[{1,1},{2,2},{3,1},{4,1},{5,1},{8,1},{11,1}].

我有以下代码

list([]) ->
  [];
list([First | Rest])  ->  
  [{First,+1} | list(Rest)].

但是我不知道我可以进行计数操作吗?谢谢

But I dont understand how it is that I can do the counting operation? Thanks

推荐答案

请参见在外壳中:

1> a:frequencies([1,5,4,5,3,2,2,8,11]).
[{1,1},{2,2},{3,1},{4,1},{5,2},{8,1},{11,1}]

您介意逐步解释此代码的工作原理

Do you mind explaining how this code works step by step

此部分:

frequencies(List) ->
    frequencies(List, #{}).

让我们通过仅将其传递给列表来调用frequencies/1函数.然后将该列表与一个空映射一起中继到frequencies/2函数,以存储结果.

let's you call the frequencies/1 function by passing it just a list. The list is then relayed to the frequencies/2 function along with an empty map to store the results.

此部分:

frequencies([H|T], Freqs) ->
    Incrementer = fun(Count) -> Count+1 end,
    NewFreqs = maps:update_with(H, Incrementer, _Default=1, Freqs),
    frequencies(T, NewFreqs).

使用模式匹配从列表H中删除第一个数字,然后调用传递第一个数字作为应在映射中更新的Key的函数maps:update_with/4. maps:update_with/4的其他自变量是要更新的映射Freqs和函数Incrementer,该函数接收与映射中的Key关联的值作为自变量. Incrementer函数的返回值是应在映射中为Key插入的新值.如果映射中不存在该密钥,则将新的密钥与_Default值一起输入到映射中.

uses pattern matching to remove the first number from the List, H, then calls the function maps:update_with/4 passing the first number as the Key that should be updated in the map. The other arguments for maps:update_with/4 are the map to be updated,Freqs, and a function, Incrementer, which receives the Value associated with the Key in the map as an argument. The return value of the Incrementer function is the new Value that should be inserted for the Key in the map. If the Key does not exist in the map, then a new Key is entered into the map with the _Default Value.

maps:update_with/4返回更新的映射NewFreqs,该映射作为参数传递给递归函数调用:

maps:update_with/4 returns the updated map, NewFreqs, which is passed as an argument to the recursive function call:

frequencies(T, NewFreqs).

第一个参数T是包含剩余数字的列表.删除列表中的所有数字后,递归函数调用将为:

The first argument, T, is a List containing the remaining numbers. When all the numbers in the List have been removed, then the recursive function call will be:

frequencies([], #{ results in this map })

该函数调用将与此函数子句匹配:

That function call will match this function clause:

frequencies([], Freqs) ->
    maps:to_list(Freqs);

maps:to_list/1将映射转换为{Key, Value}元组的列表.因为该函数子句的主体中没有递归函数调用,所以递归结束,并返回元组列表.

and maps:to_list/1 converts a map to a list of {Key, Value} tuples. Because there is no recursive function call in the body of that function clause, the recursion ends, and the list of tuples is returned.

也许不同的变量名将使代码更易于遵循:

Maybe different variable names would make the code easier to follow:

frequencies(List) ->
    frequencies(List, _ResultsMap=#{}).

frequencies([Key|Keys], ResultsMap) ->
    Incrementer = fun(Value) -> Value+1 end,
    NewResultsMap = maps:update_with(Key, Incrementer, _Default=1, ResultsMap),
    frequencies(Keys, NewResultsMap);
frequencies([], ResultsMap) ->
    maps:to_list(ResultsMap).

这篇关于Erlang,查找列表中数字出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆