本文介绍了评估形式应该在空词汇环境中评估给定形式,但我没有得到我所期望的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个特殊的变量:

Let's say I have a special var:

(defvar x 20)

然后我执行以下操作:

(let ((x 1)) (eval '(+ x 1))

其值为2。

根据CLHS,eval在当前动态环境和空词法环境中评估表单。因此,我希望得到21而不是2。

According to CLHS, eval "Evaluates form in the current dynamic environment and the null lexical environment". So, I would expect to get 21 instead of 2.

我错过了什么吗?

现在,如果我没有对符号y进行动态绑定,则进行评估

Now if I have no dynamic binding for symbol y, evaluating

(let ((y 1)) (eval '(+ y 1))

我得到条件:变量Y是未绑定的,这是有道理的,因为y没有动态绑定。

I get condition: "The variable Y is unbound", which makes sense, since there is no dynamic binding for y.

注意:我正在使用SBCL 1.0.57

Note: I'm using SBCL 1.0.57

提前感谢您的帮助!

推荐答案

在您的示例中 x special ,这意味着它绑定在动态环境

in your example x is special which means it is bound in the dynamic environment

y 不是特殊的,因此它绑定在词法环境

y is not special, so it is bound in the lexical environment

所以在第一次 eval 时,环境可以表示为:

so at the time of the first eval the environments could be represented like this:

dynamic environment:  { x : 1 } -> { x : 20, ...other global variables... } -> nil
lexical environment:  nil

符号 x 很特殊,因此 eval current 动态$ b中查找 x $ b环境并找到 x = 1

the symbol x is special so eval looks up x in the current dynamic environment and finds x = 1

假设它是与上一个示例一样,第二个 eval 的环境如下所示:

assuming it was run in same lisp as the last example, the environment of your second eval looks like this:

dynamic environment: { x : 20,  ...other global variables... } -> nil
lexical environment: { y :  1 } -> nil

符号 y 不是特殊的,因此 eval null $ b $中查找 y b词汇环境-不是当前的词汇环境-却什么也没找到。

the symbol y is not special so eval looks up y in the null lexical environment -- not the current lexical environment -- and finds nothing.

当您意识到通常会编译lisp时,这很有意义,在某些情况下,可以将词汇
环境优化为简单的 mov 指令。

this makes sense when you realize that lisp is usually compiled, and the lexicalenvironment can be optimized down to simple mov instructions in some cases.

这篇关于评估形式应该在空词汇环境中评估给定形式,但我没有得到我所期望的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:17