Yes, that wasn't very clear of me.  Here is what I was thinking when writing 
that.

In Scheme, which has general tail-call optimization, you will often find 
functions written where the programmer went out of their way to change it from 
recursive, but doesn't use tail calls, into something that recursive and uses 
tail calls.  There are sections in introductory Scheme books showing how to 
change non-tail-recursive functions into tail-recursive ones, using accumulator 
parameters.

The programmer did this because they wanted to take advantage of tail-call 
optimization and avoid using stack space linear in the depth of recursive 
calls.  This becomes almost natural after a while, and it makes sense -- you 
don't want to use linear stack space for processing long linear data structures 
when you know you could use constant space with a bit more coding effort.

I can't say this authoritatively, but I think it is far less common for 
programmers to go out of their way to change calls to other functions into tail 
calls, if they weren't originally written as tail calls.  Sets of 2 or more 
functions that are mutually recursive are less common than self-recursive ones. 
 I know that there are Scheme programs that take advantage of general tail 
calls to write state machines where each state is a separate function and they 
call each other, and other programs like that, but tail-recursive functions are 
pervasive in Scheme.

Andy

On Nov 27, 2012, at 10:12 PM, Ben Wolfson wrote:

> On Mon, Nov 19, 2012 at 6:24 PM, Andy Fingerhut
> <andy.finger...@gmail.com> wrote:
>> Clojure's loop/recur only implements tail recursion, not general tail-call
>> optimization.  tail recursion is far more commonly used in languages that
>> have it than the general tail-call optimization to arbitrary functions.
> 
> Can you explain this? I'm assuming that "having tail recursion" here
> means not "you can call yourself in tail position" but "calls to
> yourself in tail position don't grow the stack". But I don't really
> follow the second sentence, which seems to have two possible
> continuations:
> 
> (a) tail recursion is far more commonly used in languages that have it
> than the general tail-call optimization to arbitrary functions ... is
> used in languages that have tail recursion.
> (b) tail recursion is far more commonly used in languages that have it
> than the general tail-call optimization to arbitrary functions ... is
> used in languages that have *it* (i.e. general TCO).
> 
> But in (a), general tail-call optimization to arbitrary functions
> couldn't be used very much in those languages; they don't have it. And
> in (b), general TCO subsumes the optimization of tail recursion and
> comes into play whenever a procedure is called from tail position, so
> it would seem to be used pervasively in languages that have general
> TCO.
> 
> -- 
> Ben Wolfson

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