题
我是erlang的新手,所以请原谅我的天真。
我正在尝试重写我用其他语言编写的函数。其中一个是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
解决方案
我打赌你正在搜索的是来自zipwith
模块的list
函数(http://www.erlang.org/doc/man/lists.html)。
它类似于您使用的zipWith
haskell函数,并且具有类型:
zipwith(Combine, List1, List2) -> List3
.
你可能会使用类似的东西:
Count = lists:sum(lists:zipwith(fun(X, Y) -> X*Y end, A, B))
. 不隶属于 StackOverflow