From: Vojin Jovanovic
Subject: macro question
Date: 
Message-ID: <sb7t7.15$WP4.725121@newnews.cc.stevens-tech.edu>
The following

(defmacro return-self-closure (params body)
      `#'(lambda ,(cons 'self params) ,@body))

when called like this

(return-self-closure (x y) ((print x) (print y) (print self)))

expands into

#'(lambda (self x y) (print x) (print y) (print self))

and returns a closure. Now, I'd like to define a function foo that can be
called like

(foo '(x y)  '((print x) (print y) (print self)))

which uses a version of the macro above (the macro needs to be redefined,
but I don't know how) and returns the same closure as before.  Is it
possible to do this?

Thanks
Vin

From: Barry Margolin
Subject: Re: macro question
Date: 
Message-ID: <4u7t7.14$GA.3313@burlma1-snr2>
In article <···················@newnews.cc.stevens-tech.edu>,
Vojin Jovanovic <········@stevens-tech.edu> wrote:
>The following
>
>(defmacro return-self-closure (params body)
>      `#'(lambda ,(cons 'self params) ,@body))

FYI, that can be simplified a bit more:

(defmacro return-self-closure (params body)
      `#'(lambda (self ,@params) ,@body))

>when called like this
>
>(return-self-closure (x y) ((print x) (print y) (print self)))
>
>expands into
>
>#'(lambda (self x y) (print x) (print y) (print self))
>
>and returns a closure. Now, I'd like to define a function foo that can be
>called like
>
>(foo '(x y)  '((print x) (print y) (print self)))
>
>which uses a version of the macro above (the macro needs to be redefined,
>but I don't know how) and returns the same closure as before.  Is it
>possible to do this?

(defun foo (params body)
  (eval `(return-self-closure ,params ,body)))

Note, however, that if the body contains any free variable references, they
won't be closed over the current lexical environment.  Functions don't have
any access to lexical environments, only certain special operators do
(macros can expand into invocations of those operators, in order to take
advantage of this -- that's why your macro version will do the right thing
in this case).

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Frode Vatvedt Fjeld
Subject: Re: macro question
Date: 
Message-ID: <2hu1xmlxzn.fsf@dslab7.cs.uit.no>
"Vojin Jovanovic" <········@stevens-tech.edu> writes:

> Now, I'd like to define a function foo that can be called like
>
> (foo '(x y)  '((print x) (print y) (print self)))

I don't think this isn't something you are likely to really want to
do, but anyways..

  (defun foo (parameters body)
    (coerce `(lambda ,(cons 'self parameters) ,@body) 'function))

or

  (defun foo (parameters body)
    (compile nil `(lambda ,(cons 'self parameters) ,@body)))

-- 
Frode Vatvedt Fjeld