I am trying to set each element of a vector equal to 3 by looping. I get:
java.lang.ClassCastException: clojure.lang.PersistentVector can
not be cast to java.lang.Number
This is the code.
(def w [1 2 3])
(defn update [index value]
(assoc w index value))
(loop [i -1]
(if (< (count w) i)
w
(recur (update (+ i 1) 3))))
You are mixing integer and vector type by passing (update …) to recur function. The loop statement should be look like (loop [i -1 w w] ..) and then you could collect your new vector into the local “w”. If you want to use recure statement this code can help you (I guess there are a lot of other options to change values of a vector):