Im dealing with recursion in clojure, which i dont really understand. I made a small program taked from here that tries to find the smalles number that can be divided by all the numbers from 1 to 20. This is the code i wrotte, but there must be something im missing because it does not work. Could you give me a hand? thanks!
(defn smallest [nume index]
(while(not ( = index 0))
(do
(cond
(zero?(mod nume index))(let [dec' index] (smallest nume index))
:else (let [inc' nume] (smallest nume index))))))
EDIT:
Looks like is better loop/recur so i tried it:
(loop [nume 20
index 20]
(if (= index 0)
(println nume)
(if (zero?(mod nume index))
(recur nume (dec index))
(recur (inc nume) 20)))))
Working. If you are curious about result--> 232792560
whileis an imperative looping construct which in my experience one never uses more than once or twice a year.(index 20)does? Also, inrecur, the order of the values matters, I am not sure you are doing what you think you are.(index 20)is to set index to 20 again. @shlomiindexis not a function, and you are attempting to apply 20 to the functionindex. If you want to keepindexas 20, you should simply do(recur (inc nume) 20). This comment also applies to(nume), you are trying to invoke the number :). I updated my answer, see if that helps a little more.