From: Pascal Costanza
Subject: Re: Python gets macros
Date: 
Message-ID: <coiva6$ngs$1@newsreader2.netcologne.de>
Mark Carter wrote:

> I wonder if one could write something like:
> 
> (defun foo ()
>     (local-let x 0)
>     ... do something with x)
> 
> ,which looks neater to me.

You need a way to refer to the scope to which you want to add a new 
variable. Lisp's let takes the simplest approach and just opens a new scope.

In your case you seem to want to introduce the variable to the scope 
opened by the defun form. However, that's not really clear and that's 
already a sign of a not-so-optimal design. (What about defuns that are 
not at the top-level? Does your definition end there, or does it always 
expand to the top-level?)

Anyway, in case you want to inject something into the scope of the defun 
form, you have to write your own defun macro, roughly like this:

(defmacro defun* (name lambda-list &body body)
   (let ((declarations (collect-declarations body))
         (documentation (collect-documentation body))

         ;; here goes your extension:
         (local-lets (collect-local-lets body))

         ;; the following is without declarations, etc.:
         (pure-body (collect-pure-body body)))

     `(defun ,name ,lambda-list
        ,documentation
        ,@declarations
        (let (,@local-lets)
          ,@pure-body))))

...or some such. Note that the collect stuff can be turned into a loop 
that collects all these things in one pass. You can also shadow the 
original defun, so that you don't have to use the funny defun* name. In 
order to make your extension work in a more general setting, you could 
try to add it to Marco Antoniotti's definer framework - see 
http://www.bioinformatics.nyu.edu/~marcoxa/common-lisp/code.html

...or maybe you just want &aux:

(defun foo (&aux (x 0))
   ...)

;)


Pascal

-- 
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."