lisp 宏会评估它们的表达式,还是只是进行转换?
Do lisp macros ever evaluate their expressions, or do they just do transformation?
我是 lisp 的新手,我有 c 背景。我试图理解 lisp 宏。我知道他们从一组表达式到另一组表达式进行任意转换,但是他们在扩展时是否评估过他们的表达式?
可以想象一个宏递归地从传入的列表中删除一个项目,直到列表中没有剩余的元素。它必须评估列表的大小才能知道何时停止扩展。这种事情可能吗?
CL-USER 11 > (defmacro consume-list (list)
(if (null list) ; empty list?
()
(list 'consume-list (rest list)))) ; remove first element
CONSUME-LIST
CL-USER 12 > (macroexpand-1 '(consume-list (a b c))) ; expand once
(CONSUME-LIST (B C)) ; first element has been removed
T
CL-USER 13 > (macroexpand '(consume-list (a b c))) ; expand to completion
NIL ; everything has been removed
T
我是 lisp 的新手,我有 c 背景。我试图理解 lisp 宏。我知道他们从一组表达式到另一组表达式进行任意转换,但是他们在扩展时是否评估过他们的表达式?
可以想象一个宏递归地从传入的列表中删除一个项目,直到列表中没有剩余的元素。它必须评估列表的大小才能知道何时停止扩展。这种事情可能吗?
CL-USER 11 > (defmacro consume-list (list)
(if (null list) ; empty list?
()
(list 'consume-list (rest list)))) ; remove first element
CONSUME-LIST
CL-USER 12 > (macroexpand-1 '(consume-list (a b c))) ; expand once
(CONSUME-LIST (B C)) ; first element has been removed
T
CL-USER 13 > (macroexpand '(consume-list (a b c))) ; expand to completion
NIL ; everything has been removed
T