Subject: Re: compiler optimization techniques
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 10 Apr 2001 10:37:02 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9aunoe$8nuhs$1@fido.engr.sgi.com>
Shriram Krishnamurthi <sk+10@cs.brown.edu> wrote:
+---------------
| "Ji-Yong D. Chung" <virtualcyber@erols.com> writes:
| > It is properly tail recursive -- it was easy to implement this.  I have
| > checked the scheme stack on my interpreter, and it does not grow
| > when a function calls itself with various types of tail calls.
| 
| That isn't enough.  You should also handle tail *calls*, ie, calls to
| arbitrary procedures in tail position.  That's critical for functional
| programming!
+---------------

And not just functional programming, but certain styles of imperative
programming as well. E.g., I often implement finite-state machines in
Scheme with a procedure per state, with state transitions implemented
as a tail call from the "current" state procedure to the "next" state
procedure. It results in a very natural, readable, and easily maintained
style of code, much nicer than the "while (state!=DONE) switch (state)"
idiom usually used in C code, which rapidly becomes totally unreadable
when the number of states or transitions gets large.

Implementing the full tail-call optimization given in R5RS's "3.5 Proper
tail recursion" is essential for the procedure-per-state style to work,
especially for long-running state machines.


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
SGI Network Engineering		<URL:http://reality.sgi.com/rpw3/>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA