Question

I am new to Erlang, so please forgive my naïvety.

I am trying to re-write functions that I have written in other languages. One of them is the jaccard bit index.

in python haskell and clojure it would work as follows:

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))))

I think my problem is that I only know of Erlang's

map(Fun, List1) -> List2

and every time I have done it before I have used somethig similar to:

map(Fun, List1, List2) -> List3

Was it helpful?

Solution

I bet what you are searching is the zipwith function from the list module (http://www.erlang.org/doc/man/lists.html). It is similar to the zipWith haskell function you used, and has type:

zipwith(Combine, List1, List2) -> List3

You will probably use something like:

Count = lists:sum(lists:zipwith(fun(X, Y) -> X*Y end, A, B))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top