Subject: Re: Iteration in lisp
From: (Rob Warnock)
Date: Tue, 22 Apr 2008 04:48:29 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
Bob Felts <> wrote:
| Kent M Pitman <> wrote:
| > A recursive function can run out of stack since CL does not guarantee
| > tail call elimination. 
| Any pointers to the rationale for this decision?

In addition to what others have replied, there are many constructs
in CL for which it's either difficult to "do the right thing" with
a tail call or even know *what* "the right thing" would be if one
were trying to perform tail-call optimization, for example:

- Tail-calling out of blocks in which dynamic variables were rebound.

- Tail-calling out of blocks wrapped by UNWIND-PROTECT.

- Tail-calling out of blocks wrapped by HANDLER-CASE or HANDLER-BIND.
  [IGNORE-ERRORS is the same as HANDLER-CASE.]

- Non-local exits from exception handlers or restarts.

- Heck, non-local exits, period!

And even in the simple cases, it's a debatable question whether the
tail-call optimization has to be *completely* "safe-for-space" or
merely "safe-for-stack". The difference has to do with references
to dynamically "dead" data which might appear to still be lexically
"live" due to the way closure environments are constructed. See the
plentiful published Scheme & ML literature for more on this.[1]

Also see <
"CL Implementations and Tail-Call Elimination", from September 2007,
in which I developed some simple CL macrology for allowing "cooperating
tail-call groups" of functions to share a loop/trampoline emulation of
true tail-call optimization. [But note that this is still not necessarily
completely "safe-for-space", depending on your implementation, nor does it
"do the right thing" in the exceptional cases mentioned above!!]


[1] A couple of refs to get you started:
       "Efficient and Safe-for-Space Closure Conversion".
       Zhong Shao & Andrew Appel
       [In ACM Transactions on Programming Languages and
       Systems (TOPLAS), 22(1), pages 129-161, January 2000.]
       "Proper tail recursion and space efficiency"
       William D. Clinger
       [In the Proceedings of the 1998 ACM Conference on Programming
       Language Design and Implementation, June 1998, pages 174-185.]

Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607