如何评估宏内兰伯达内部的形式?
我在以下宏方面有问题:
(defmacro gather-params (&rest body)
"Return plist of params"
`(concatenate 'list
(map 'list
#'(lambda (plist)
(if (typep (first plist) 'keyword)
(cons 'list plist)
plist))
',body)))
在宏内,我无法通过在其前面添加逗号来评估PLIST,例如:,当我这样做时,编译器会抱怨变量,PLIST不存在。
我对宏内的范围不了解吗?
当前结果:
input: (gather-params (:mykey (+ 1 1)) (list 1 2 3))
result: ((LIST :MYKEY (+ 1 1)) (LIST 1 2 3))
所需结果:
input: (gather-params (:mykey (+ 1 1)) (list 1 2 3))
result: ((LIST :MYKEY 2) (LIST 1 2 3))
I'm having problems with the following macro:
(defmacro gather-params (&rest body)
"Return plist of params"
`(concatenate 'list
(map 'list
#'(lambda (plist)
(if (typep (first plist) 'keyword)
(cons 'list plist)
plist))
',body)))
Within the macro, I can't make plist evaluate by adding a comma in front of it, e.g: ,plist when I do that, the compiler complains that the variable ,plist does not exist.
Is there something I'm not understand about the scope within a macro?
Current result:
input: (gather-params (:mykey (+ 1 1)) (list 1 2 3))
result: ((LIST :MYKEY (+ 1 1)) (LIST 1 2 3))
Desired result:
input: (gather-params (:mykey (+ 1 1)) (list 1 2 3))
result: ((LIST :MYKEY 2) (LIST 1 2 3))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(condenate'list'(1 2 3))
只是(copy -list'(1 2 3))
- >两者都评估
(1 2 3)
。宏生成代码。它有一个反引用的列表。此反调用列表是在宏扩展时间计算的 - >当宏扩展发生时,例如在汇编过程中。
生成的代码具有称为
plist
的参数的函数。此功能在运行时执行。它在宏扩展过程中不存在。因此,plist
在宏观扩展过程中没有变量。因此,您无法在宏观上的plist
的值计算,因为那时该变量不存在。如果要评估代码,则Common LISP为此具有函数
eval
。例如,可以在运行时调用eval
。使用不同的方法查看此示例:
(concatenate 'list '(1 2 3))
is just(copy-list '(1 2 3))
-> which both evaluate to
(1 2 3)
.The macro generates code. It has a backquoted list. This backquoted list is computed at macroexpansion time -> when the macro expansion happens, for example during compilation.
The generated code has a function with a parameter called
plist
. This function executes at runtime. It does not exist during macroexpansion. Thusplist
is no variable during macroexpansion. Thus you can't compute a value ofplist
during macroexpansion, since the variable does not exist then.If you want to evaluate code, then Common Lisp has the function
eval
for that. One can for example calleval
at runtime.Check out this example, with a different approach: