Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!cornell!batcomputer!itsgw!steinmetz!akhenaten!caldwell
From: caldwell@akhenaten.steinmetz (James Caldwell)
Newsgroups: comp.lang.lisp
Subject: Re: nested function definitions
Message-ID: <11314@steinmetz.ge.com>
Date: 22 Jun 88 14:51:40 GMT
References: <5862@spool.cs.wisc.edu> <5568@utah-cs.UUCP>
Sender: news@steinmetz.ge.com
Reply-To: caldwell@akhenaten.steinmetz.ge.com (James Caldwell)
Organization: General Electric CRD, Schenectady, NY
Lines: 43

In article <5568@utah-cs.UUCP> sandra@utah-cs.UUCP (Sandra J Loosemore) writes:
>In article <5862@spool.cs.wisc.edu>, engber@speedy.cs.wisc.edu (Mike Engber) writes:
>> What does nesting function definition do in Common Lisp? For example: ...
>
>Strictly speaking, Common Lisp does not (yet) support DEFUN except at
>"top-level" locations; see CLtL p. 66.  ...
>
Steele leaves it open, see CLtL p. 67, where he describes what
the proper lexical environment should be for a defun form that does
not appear at the top-level.

In their interpreters, Lucid and Franz both correctly handle the case

(let ((x 1))
  (defun y () x))

returning 1 when y is called.  Unfortunately they complain if you try
to compile y since y's lexical environment is not empty.  Nesting 
defuns within the scope of a let is very useful for defining variables
and functions that are global to a couple of named functions.  This
technique is used a number of times in Charniak, Riesbeck, McDermott
and Meehan's book _Artifical_Intelligence_Programming_.   

Of course the same effect can be had by collecting those couple of
functions into their own package, defvaring the variables and defuning
the functions and then exporting only the desired named functions.  A
lexical closure is a simpler and more elegant mechanism and avoids a
proliferation of packages.

>... there is currently a proposal before X3J13 ...  Under this
>proposal, DEFUN would always set the *global* function definition of
>the symbol ... 

I assume the proposal is intended to allow named functions to be
defined with a non-empty lexical environment.  Sounds good to me.

>-Sandra Loosemore (sandra@cs.utah.edu)

  Jim Caldwell     caldwell%akhenaten@steinmetz.UUCP
                   uunet!steinmetz!akhenaten!caldwell

  Jim Caldwell     caldwell%akhenaten@steinmetz.UUCP
                   uunet!steinmetz!akhenaten!caldwell