David Nolen <dnolen.li...@gmail.com> writes: > The n has to be held onto to complete the computation, so the tail > call optimization cannot be performed.
That's right. > ;; can be tail call optimized > (define (add-tail n) > (add-iter n 0)) > > (define (add-iter iter result) > (if (= iter 0) > result > (add-iter (- iter 1) (+ result 1)))) The equivalent Clojure code works with fine without blowing the stack. These sort of simple tail recursion calls work quite well in Clojure. (defn add-iter [iter result] (if (= iter 0) result (recur (- iter 1) (+ result 1)))) (defn add-tail [n] (add-iter n 0)) ;; Another way (defn add-loop [n] (loop [n n acc 0] (if (zero? n) acc (recur (dec n) (inc acc))))) I generally use loop where I would use a named let in Scheme. Cheers, Chris Dean --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---