1
votes

i'm new to clojure and functional programming so i would appreciate some help. :)

What i'm trying to do is : I'm trying to get this [1 1 2] from this "structure" [[1 0 1] [0 1 0] [0 0 1]] therefore i'm trying to sum first elements of the three vectors, second elements of the three vectors and finally third elements of the three vectors to get a new vector in the end containing this sums.

My code is the following:

(def matrix [[1 0 1] [0 1 0] [0 0 1]])
(def sum (atom 0))
(def sum-vector (atom []))
    (for [i (range 3)]                
       ((for [j (range (count matrix))]
               (reset! sum (+ @sum (nth (nth matrix j) i)))
                     ) 
               (reset! sum-vector (conj @sum-vector @sum))
               (reset! sum 0)
                 ))

But i get this exception : clojure.lang.LazySeq cannot be cast to clojure.lang.IFn ... What i'm i doing wrong?

1

1 Answers

3
votes

The problem is ((for ....)) That says evaluate the (for ....) form, and then take that result and execute it. The (for ...) is returning a LazySeq, instead of a function.

(apply map + [[1 0 1] [0 1 0] [0 0 1]])
;; (1 1 2)

If you want a vector returned instead of a LazySeq:

(apply mapv + [[1 0 1] [0 1 0] [0 0 1]])
;; [1 1 2]