4
我有兩個列表:L和E我嘗試寫一個函數,它具有從低出現次數爲元素的個數返回另一個列表E.如何在Erlang中使用閉包?
-module(mymodule).
-export([count/2]).
-export([numberOfOccurences/2]).
count([Head|Tail], Counter) ->
fun(Element) -> if
[Head|Tail] == [] -> Counter;
Element == Head -> count(Tail, Counter + 1);
Element /= Head -> count(Tail, Counter)
end
end.
numberOfOccurences(L, E) ->
lists:map(count(L, 0), E).
mymodule:numberOfOccurences[1,2,"abc",2,1,"abc",4,1,1], [1,2,3,"abc"])
應該回報[4,2,0,2]
。但它返回一個包含4個函數的列表。我究竟做錯了什麼?