The examples given for Scheme and Common Lisp also blow the stack (I checked the Scheme example in PLT Scheme). Consider the following: ;; cannot be tail call optimized (define (add n) (if (= n 0) 0 (+ 1 (add (- n 1)))))
;; 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 examples given for Scheme and Common Lisp both need to preserve the entire stack to complete the computation, just as Clojure needs to because the final expression looks like: (- n ...) The n has to be held onto to complete the computation, so the tail call optimization cannot be performed. Would love to hear from TCO experts if I'm off base here. On Wed, Jun 10, 2009 at 11:45 AM, arasoft <t...@arasoft.de> wrote: > > I know, but wouldn't it be nice to show that using Clojure you can > write a solution that "really" works? > > On Jun 10, 5:38 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote: > > I don't see the solutions provided for other languages solving the > > tail call problem. And I also don't see the problem explictly stating > > that a tail call optimized version is required. > > > > It seems rather to me that the page just want to exhibit that the > > language allows mutual recursion of calls, period ? > > > > So your first initial version seems sufficient. Look for example at > > the java provided version, it suffers from the same StackTrace > > problem. > > > > 2009/6/10 arasoft <t...@arasoft.de>: > > > > > > > > > Thank you for the answers. I now understand why this cannot work with > > > trampoline. > > > Is there another way to avoid stack overflow? I'd like to submit the > > > code tohttp://rosettacode.org/wiki/Mutual_Recursionto improve the > > > Clojure coverage and also to learn more stuff myself... > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---