From: Christopher Thomas
Subject: Evaluating in given environment?
Date: 
Message-ID: <1992Nov23.025931.27024@beaver.cs.washington.edu>
I have a problem to solve in Common Lisp, and a solution that seems to work.
I am posting this to get feedback on the solution.

Problem: I have a macro that takes a form as an argument, and the expansion
passes that form to a function.  I want the function to evaluate the form,
but in the lexical environment of the macro expansion.  Here is a simplified
example of my problem:

  (defmacro eval-it (form &environment env)
    `(function-eval-it ',form ',env))

   ... Meanwhile, in another lexical environment...

  (defun function-eval-it (form &rest env)
    (evalhook form nil nil env))

  (setf x 40)
  (let ((x 10))
    (eval-it (print x)))

   10
   => 10

As I said, this seems to do the right thing, but, I am wondering
if there is a more elegant way to do this, such as a better way to
evaluate the form, rather than EVALHOOK.  Am I using EVALHOOK properly?

Thanks in advance,

**************************************************************************
* Christopher Thomas             * What do you mean, an African swallow, *
* Univ. of WA Computer Science   *    or a European swallow?             *
* ·······@cs.washington.edu      *       - King Arthur                   *
**************************************************************************


-- 
**************************************************************************
* Christopher Thomas             * What do you mean, an African swallow, *
* Univ. of WA Computer Science   *    or a European swallow?             *
* ·······@cs.washington.edu      *       - King Arthur                   *
From: Barry Margolin
Subject: Re: Evaluating in given environment?
Date: 
Message-ID: <1epkbrINN1g8@early-bird.think.com>
In article <······················@beaver.cs.washington.edu> ·······@cs.washington.edu (Christopher Thomas) writes:
>Problem: I have a macro that takes a form as an argument, and the expansion
>passes that form to a function.  I want the function to evaluate the form,
>but in the lexical environment of the macro expansion.  

You should package it up into a lambda expression, and use the FUNCTION
special form to capture the lexical environment.

>  (defmacro eval-it (form &environment env)
>    `(function-eval-it ',form ',env))

(defmacro eval-it (form)
  `(function-eval-it #'(lambda () (progn ,form))))

(defun function-eval-it (funarg)
  (funcall funarg))

>  (defun function-eval-it (form &rest env)
>    (evalhook form nil nil env))

FYI, that should be "&optional env", shouldn't it?  That isn't even
guaranteed to work for your purposes, since &environment only captures the
macro environment, not the full lexical environment (e.g. variable bindings
aren't necessarily there).
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar