Tail-recursive just means the recursive call occurs in tail position: the 
result of the recursive call gets returned as-is rather than having some 
additional computations done to it first. This means the compiler *could* 
optimize the recursive call to not consume any additional stack space, by 
replacing the current stack frame rather than pushing a new one.

However, the JVM does not support tail-call optimization. Apparently Clojure 
can't support implicit TCO without support from the JVM, so it requires that 
you use the explicit 'recur' construct to allow it to simulate TCO in the 
self-recursive case. Recur can only occur in tail position, and Clojure will 
give you an error if you try to use it elsewhere.

On Jan 26, 2011, at 9:04 AM, Harrison Maseko wrote:

> Hi all,
> I need some help in understanding some basic concept. The book
> Programming Clojure on pages 134 - 136 deals with tail recursion and
> self-recursion using recur. The tail recursive example blows the stack
> while the self-recursive function using recur presented on page 135
> does not. On page 136 the book says that "the critical difference
> between tail-fibo (tail recursion) and recur-fibo (self-recursion
> using recur) is on line 7, where recur replaces the call to fib." Why
> does recur make such a difference in the way the function consumes
> resources? What really is the difference between a tail-recursive
> function and a recursive function using recur?
> Thanks for your help.
> -h.
> 
> -- 
> 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

-- 
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

Reply via email to