Path: utzoo!attcan!uunet!husc6!mailrus!utah-gr!utah-cs!sandra
From: sandra@utah-cs.UUCP (Sandra J Loosemore)
Newsgroups: comp.lang.lisp
Subject: Re: nested function definitions
Message-ID: <5568@utah-cs.UUCP>
Date: 21 Jun 88 13:34:24 GMT
References: <5862@spool.cs.wisc.edu>
Organization: University of Utah CS Dept
Lines: 27

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:
> 
> (defun cube (x)
>  (defun square (x)
>   (* x x))
>  (* x (square x)))
> 
> I saw this style of coding in an article. I played around with it using
> Allegro Common Lisp (on a Mac) and it seems to create some sort of closure.
> It also seems to me that this is a more inefficient way to do "PASCAL"
> style nesting than using FLET. Does anyone what this nesting really does?

Strictly speaking, Common Lisp does not (yet) support DEFUN except at
"top-level" locations; see CLtL p. 66.  However, there is currently a
proposal before X3J13 (the ANSI Common Lisp standardization committee)
to clarify and extend the language on this point.  Under this proposal,
DEFUN would always set the *global* function definition of the symbol
(unlike FLET, which establishes a lexical function binding).

In your example above, the symbol-function of SQUARE would be setf'ed
each time CUBE is entered.  Since no variables that are closed over are
referenced inside the body of SQUARE, the function would not necessarily
have to be represented as a closure.  Note that the functional binding
of SQUARE persists after CUBE has been exited.

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