【问题标题】:erlang map functionerlang映射函数
【发布时间】:2012-07-26 23:50:22
【问题描述】:

我是 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

【问题讨论】:

    标签: map erlang arity


    【解决方案1】:

    我敢打赌,您正在搜索的是 list 模块 (http://www.erlang.org/doc/man/lists.html) 中的 zipwith 函数。 它类似于您使用的zipWith haskell 函数,类型为:

    zipwith(Combine, List1, List2) -> List3
    

    你可能会使用类似的东西:

    Count = lists:sum(lists:zipwith(fun(X, Y) -> X*Y end, A, B))
    

    【讨论】:

    • 这看起来正是我想要的!
    猜你喜欢
    • 2017-11-22
    • 2011-06-10
    • 1970-01-01
    • 2015-07-25
    • 2012-06-17
    • 2019-02-14
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多