0

I have 3 vectors [1 2 3] [4 5 6] [7 8 9. I want to add the vectors by indices ie return a vector

[(+ 1 4 7) (+ 2 5 8) (+ 3 6 9)] => [12 15 18]

I thought if doing something like this but I realise I'm not getting the vector out

(defn vec-adder [x y z]
  (loop [a 1]
    (when (< a (count x)
      (recur (+ (nth x a) (nth y a) (nth z a)) (+ a 1)))))

Any help would be much appreciated. Thanks.

0

1 Answer 1

8
(mapv + [1 2 3] [4 5 6] [7 8 9])

+ is a function that can take any number of arguments (it is multi-arity).

Sign up to request clarification or add additional context in comments.

1 Comment

... or, upon a general sequence of vectors: #(apply mapv + %). For example, (#(apply mapv + %) [[1 2 3] [4 5 6] [7 8 9]]) => [12 15 18].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.