erlang地图功能

beo*_*ver 1 erlang map arity

我是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))))
Run Code Online (Sandbox Code Playgroud)

我想我的问题是我只知道Erlang的

map(Fun, List1) -> List2

每次我使用之前我都用过类似的东西:

map(Fun, List1, List2) -> List3

ton*_*nio 5

我打赌你正在搜索的是模块中的zipwith功能list(http://www.erlang.org/doc/man/lists.html).它类似于zipWith您使用的haskell函数,并且具有以下类型:

zipwith(Combine, List1, List2) -> List3
Run Code Online (Sandbox Code Playgroud)

您可能会使用以下内容:

Count = lists:sum(lists:zipwith(fun(X, Y) -> X*Y end, A, B))
Run Code Online (Sandbox Code Playgroud)