<p>I have an s-expression bound to a variable in Common Lisp:</p>

<pre><code>(defvar x '(+ a 2))
</code></pre>

<p>Now I want to create a function that when called, evaluates the expression in the scope in which it was defined.  I've tried this:</p>

<pre><code>(let ((a 4))
  (lambda () (eval x)))
</code></pre>

<p>and</p>

<pre><code>(let ((a 4))
  (eval `(lambda () ,x)))
</code></pre>

<p>But both of these create a problem: EVAL will evaluate the code at the top level, so I can't capture variables contained in the expression.  Note that I cannot put the LET form in the EVAL.  Is there any solution?</p>

<p>EDIT: So if there is not solution to the EVAL problem, how else can it be done?</p>

<p>EDIT: There was a question about what exactly I am try to do.  I am writing a compiler.  I want to accept an s-expression with variables closed in the lexical environment where the expression is defined.  It may indeed be better to write it as a macro.</p>

CLISP implements an extension to evaluate a form in the lexical environment. From the fact that it is an extension, I suspect you can't do that in a standard-compliant way.
```
```
(ext:eval-env x (ext:the-environment))
```
```
See
http://clisp.cons.org/impnotes.html#eval-environ
.