erlang map函数
问题描述:
我是Erlang的新手,所以请原谅我的天真。erlang map函数
我正在尝试重写我用其他语言编写的函数。其中之一是jaccard位索引。
在python Haskell和Clojure中,将工作方式如下:
xs = [1,1,0,0,1,1,0,0,1,1,0,0]
ys = [1,0,1,0,1,0,1,0,1,0,1,0]
# python 3.X
def jaccard_bit_index(A,B):
i = sum(map(operator.mul ,A,B))
return i/(sum(A) + sum(B) - i)
-- haskell
jaccrd_bit_index a b =
count/((sum a) + (sum b) - count)
where
count = sum $ zipWith (*) a b
%% clojure
(defn jaccard-bit-index [a b]
(let [binarycount (apply + (map * a b))]
(/ binarycount
(- (+ (apply + a) (apply + b))
binarycount))))
我想我的问题是,我只知道Erlang的
map(Fun, List1) -> List2
每一次之前,我已经做到了我用过一些类似于:
map(Fun, List1, List2) -> List3
答
我敢打赌,您正在搜索的是list
模块的zipwith
函数(http://www.erlang.org/doc/man/lists.html)。 它类似于您使用zipWith
哈斯克尔功能,且类型:
zipwith(Combine, List1, List2) -> List3
你可能会使用这样的:
Count = lists:sum(lists:zipwith(fun(X, Y) -> X*Y end, A, B))
看起来像正是我期待的! – beoliver 2012-07-28 16:40:13