I'm new to clojure as well, so someone please correct me if the following explanation is wrong, but I'll give it a shot :).
The JVM does not natively support TCO; clojure uses the recur form to accomplish what is effectively the same thing despite that shortcoming of its host. The recur form works by jumping back to either the loop (or, if there is no loop form, the defn/fn), and rebinding those scoped variables to the value of the recur's arguments. In your example: (loop [n number factorial 1] ;; This will be the "target" of the recur (if (zero? n) ;; If n is zero, factorial ;; return the value of factorial (recur (dec n) (* factorial n)))) ;; otherwise, jump to the loop, rebinding n to (dec n) and factorial to (* factorial n) I hope that this made sense :). On Sunday, April 15, 2012 11:37:35 AM UTC-4, Anto wrote: > > I'm very new to Clojure. And I was pretty much interested to learn > about vectors, list . maps in clojure. > > I have a code like this : > > (defn factorial-1 [number] > "computes the factorial of a positive integer > in a way that doesn't consume stack space" > (loop [n number factorial 1] > (if (zero? n) > factorial (recur (dec n) (* factorial n))))) > > (println (time (factorial-1 5))) > > > which argues that it does tail recursion optimization. I found this > code on net. Can anyone explain me what does the last three lines > does: > > (loop [n number factorial 1] > (if (zero? n) > factorial (recur (dec n) (* factorial n)))) > > Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en