Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news.harvard.edu!news2.near.net!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!vixen.cso.uiuc.edu!csrd.uiuc.edu!sp10.csrd.uiuc.edu!harrison
From: harrison@sp10.csrd.uiuc.edu (Luddy Harrison)
Subject: Re: Scheme _in_ Emacs?
In-Reply-To: tfb@cogsci.ed.ac.uk's message of Mon, 5 Sep 1994 12:25:30 GMT
Message-ID: <HARRISON.94Sep5085451@sp10.csrd.uiuc.edu>
Followup-To: comp.lang.scheme
Sender: news@csrd.uiuc.edu
Organization: UIUC Center for Supercomputing Research and Development
References: <RAMSDELL.94Aug17063434@triad.mitre.org> <TTHORN.94Aug17150829@ceres.daimi>
	<RAMSDELL.94Aug24085039@triad.mitre.org>
	<WGD.94Aug25012615@martigny.ai.mit.edu>
	<TFB.94Aug31020302@oliphant.cogsci.ed.ac.uk>
	<WGD.94Sep3205634@martigny.ai.mit.edu>
	<TFB.94Sep5132531@burns.cogsci.ed.ac.uk>
Date: 05 Sep 1994 13:54:50 GMT
Lines: 48

* William G Dubuque wrote:
> (Tim Bradshaw) writes:
>    From: tfb@cogsci.ed.ac.uk (Tim Bradshaw)

>    * William G Dubuque wrote:
>> Could someone clarify just why they think it is impossible to obtain
>> proper tail-recursion in GNU Emacs lisp? Perhaps the concern is about
>> efficiency vs. implementability.

>>>What I meant was is that there is no way for an implementation of a
>>>language with elisp's semantics (ie dynamic scope) to *optimize* tail
>>>calls in the way that scheme requires.  You can't optimize a tail call
>>>by throwing away the stack frame of the function because the function
>>>you call can legitimately refer to the bindings you have made.  That
>>>was what my example was doing: the tail call to scumble can't be
>>>optimized.  That call could equally well be a recursive call to foo of
>>>course:
>>>
>>>    (defun foo (rec)
>>>      (if rec
>>>	  bar
>>>	(let ((bar 3))
>>>	  (foo t))))
            
A similar effect can be achieved in Scheme using closures:

(define foo (lambda (x f)
   (if (> x 1000)
       (f)
       (let ((bar (+ x 1)))
         (foo bar (lambda () (* bar 10)))))))

so that the objection

	You can't optimize a tail call
	by throwing away the stack frame of the function because the function
	you call can legitimately refer to the bindings you have made.

would apply to Scheme as well, if variable bindings were stored on the stack
in general.

In my opinion it has never been made adequately clear precisely what
the standard mandates in the way of tail-recursion optimization.  Some
time ago I asked for a clarification, but as I recall, no very
satisfactory conclusion was reached.

-Luddy Harrison
harrison@csrd.uiuc.edu
