区分绑定变量和未绑定变量
Distinguishing Bound from Unbound Variables
我正在尝试编写一个名为 different
的宏来测试两个用户提供的参数是否是临时的 eq
,其中参数可能是绑定的或未绑定的。但是我迷失了可能性(也许还有逻辑)。以下似乎有效,但需要增强(包括避免变量捕获和多重评估):
(defmacro different (item1 item2)
`(not (eq (if (boundp ',item1) ,item1 ',item1)
(if (boundp ',item2) ,item2 ',item2))))
基本思想是寻找任何未绑定的变量,引用它,然后查看它是否是 eq
到另一个变量的值。 (目标是让最终用户不必决定何时引用参数,因为绑定变量以其他方式标记。)
所以现在:
if x is unbound and y is bound to 'x, or
y is unbound and x is bound to 'y
(different x y) => NIL
if x is unbound and y is bound to 'z, or
y is unbound and x is bound to 'z
(different x y) => T
主要问题是 item1 或 item2 可以是任意 lisp 对象的指示符(在这种情况下 equalp
将替代 eq
)。例如:
(defparameter x 3)
(different x 3) => NIL (since they are equalp)
(defparameter x '(a b c))
(different x (c b a)) => T (where (c b a) gets quoted)
这可以分解到宏中吗,if 语句可以放在反引号之外吗?
只有六个案子需要处理
这是要完成的工作的映射:
For b ≡ bound symbol
For u ≡ unbound symbol
For e ≡ any other value
b b -> eq
b u -> equalp
u u -> equalp
e e -> equalp
e u -> ERROR (makes no sense)
e b -> equalp
希望这能帮助您组织分支逻辑。当我遇到这样的爆炸时,我喜欢拉一些纸并通过分支进行工作。通常可以使用谓词演算来减少它,或者想出另一种分支更少的表示。
我正在尝试编写一个名为 different
的宏来测试两个用户提供的参数是否是临时的 eq
,其中参数可能是绑定的或未绑定的。但是我迷失了可能性(也许还有逻辑)。以下似乎有效,但需要增强(包括避免变量捕获和多重评估):
(defmacro different (item1 item2)
`(not (eq (if (boundp ',item1) ,item1 ',item1)
(if (boundp ',item2) ,item2 ',item2))))
基本思想是寻找任何未绑定的变量,引用它,然后查看它是否是 eq
到另一个变量的值。 (目标是让最终用户不必决定何时引用参数,因为绑定变量以其他方式标记。)
所以现在:
if x is unbound and y is bound to 'x, or
y is unbound and x is bound to 'y
(different x y) => NIL
if x is unbound and y is bound to 'z, or
y is unbound and x is bound to 'z
(different x y) => T
主要问题是 item1 或 item2 可以是任意 lisp 对象的指示符(在这种情况下 equalp
将替代 eq
)。例如:
(defparameter x 3)
(different x 3) => NIL (since they are equalp)
(defparameter x '(a b c))
(different x (c b a)) => T (where (c b a) gets quoted)
这可以分解到宏中吗,if 语句可以放在反引号之外吗?
只有六个案子需要处理
这是要完成的工作的映射:
For b ≡ bound symbol
For u ≡ unbound symbol
For e ≡ any other value
b b -> eq
b u -> equalp
u u -> equalp
e e -> equalp
e u -> ERROR (makes no sense)
e b -> equalp
希望这能帮助您组织分支逻辑。当我遇到这样的爆炸时,我喜欢拉一些纸并通过分支进行工作。通常可以使用谓词演算来减少它,或者想出另一种分支更少的表示。