如何将列表传递给通用Lisp中的宏? [英] How to pass a list to macro in common lisp?
问题描述
我正在尝试将列表传递给宏,例如:
(defmacro print-lst(lst)
`(progn
,@(mapcar#'(lambda(x)`(print,x))lst)))
(let((lst'(1 2 3)))
(print-lst lst))
发现错误: LST值不等于
所以,我的问题是,这段代码有什么问题,以及如何将列表传递给宏?
'LST
不是列表,因此不是 MAPCAR
。 您可以将其称为(print-lst(1 2 3))
,但是您可以不使用宏而只做(mapc#'print lst)
I'm trying to pass a list to macro, for example:
(defmacro print-lst (lst)
`(progn
,@(mapcar #'(lambda (x) `(print ,x)) lst)))
(let ((lst '(1 2 3)))
(print-lst lst))
It caught error: "The value LST is not of type LST".
So, my question is, what's wrong with this piece of code and how to pass list to macro?
I'm not sure why you want to define this as a macro instead of a regular function, but the problem is that macros do not evaluate their arguments. If you give it the name of a lexical variable, all it sees is the name ('LST
), not the bound value. It is complaining (correctly) that the symbol 'LST
is not a list, and thus not a valid second argument to MAPCAR
.
You could call it as (print-lst (1 2 3))
, but then you could do without the macro and just do (mapc #'print lst)
这篇关于如何将列表传递给通用Lisp中的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!