Re: tail call optimization
- From: Ulrich Hobelmann <u.hobelmann@xxxxxx>
- Date: Wed, 27 Apr 2005 10:28:27 -0500
Bruno Haible wrote:
It's always a nice exercise to write such small functions yourself, in a recursive way:
(defun delete-nth-element (n l) (labels ((next (n l res) (if (null l) (reverse res) (next (1- n) (cdr l) (if (= n 0) res (cons (car l) res)))))) (next (1- n) l '())))
A good compiler will flatten this into a loop.
Your notion of "good compiler" would not be useful in practice. Namely, you are only thinking at the optimization quality of a compiler, not at the debuggability of the resulting code.
If the compiler turns tail calls into recursion, some of the stack frames are not present any more at runtime. Which means, the developer, when looking at the stack trace, doesn't have the complete info.
Isn't that what traces are for (the complete info)? And stepping through the code (I'm not familiar with Lisp debugging yet, but I assume there is a stepping facility like in gdb for C) should also work just fine, since it's a loop after all.
[...]
The conclusion is: Code that assumes tail recursion elimination makes it _impossible_ to _debug_ real big testcases.
Maybe.
Write it iteratively instead, and debuggability and testability are reconciled.
The trouble is, not everything can nicely be written with loops. It might end up really awkward and you have to use trampolines and stuff, just like in Java or C.
--
No man is good enough to govern another man without that other's consent. -- Abraham Lincoln
.
- References:
- problem setfing with nthcdr
- From: André Thieme
- Re: problem setfing with nthcdr
- From: Pascal Bourguignon
- Re: problem setfing with nthcdr
- From: André Thieme
- Re: problem setfing with nthcdr
- From: Matthias Buelow
- tail call optimization (was: Re: problem setfing with nthcdr)
- From: Bruno Haible
- problem setfing with nthcdr
- Prev by Date: Re: can anyone offer Lisp job?
- Next by Date: Re: Lisp on AMD 64
- Previous by thread: tail call optimization (was: Re: problem setfing with nthcdr)
- Next by thread: Re: tail call optimization (was: Re: problem setfing with nthcdr)
- Index(es):
Relevant Pages
|