Wrong number of args (0) passed to: PersistentVector on loop/recur function

Let me move the whitespace in your code so it’s obvious to you what is wrong:

(defn factors [x] 
  ((loop [n x i 2 acc []]
     (if (prime? n) (conj acc n)
         (if (zero? (mod n i)) (recur (/ n i) 2 (conj acc i))
             (recur n (inc i) acc))))))

You see that weird (( at the start of your function? What’s that all about? Remember that in Clojure, as in lisps in general, parentheses are not a grouping construct! They are a function-call mechanism, and you can’t just throw extras in for fun. Here, what you wrote has the following meaning:

  1. Run this loop that will compute a vector.
  2. Call the resulting value as a function, passing it no arguments.

Leave a Comment