function recursion in clojure -


im dealing recursion in clojure, dont understand. made small program taked here tries find smalles number can divided numbers 1 20. code wrotte, there must im missing because not work. give me 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 better loop/recur 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 curious result--> 232792560

while not think does.

in clojure, (well, almost) immutable, meaning if index 0, 0 in same context. looping until 1 makes little sense.

there number of ways achieve trying do, first, , trivial (i think!) newcomers, understand loop/recur. example:

(loop [counter 0]    (when (< counter 10)      (println counter)     (recur (inc counter)))) 

in here, counter defined 0, , never changes in usual way. when hit recur, submit new value, in case increment of previous counter, brand new iteration starting @ loop, counter bound 1.

edit: notice however, example return nil. used side effect of println. why return nil? because in last iteration, when clause return nil. if want return else, should perhaps use if , specify returned @ last iteration.

you should read little more paradigm, , perhaps exercises 4clojure better grasp @ this. once do, become simpler think in way, , tremendous benefits of style begin emerge.

good luck!


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -