我正在尝试编写一个名为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语句是否可以放在反引号之外?
发布于 2019-09-05 03:31:02
只有六个案子要处理
下面是要做的事情的映射:
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希望这能帮助你组织你的分支逻辑。当我遇到像这样的爆炸时,我喜欢抽出一些纸来做分支工作。通常,可以使用谓词演算减少它,或者提出具有较少分支的另一种表示。
https://stackoverflow.com/questions/57749449
复制相似问题