Why is the @ sign needed in this macro definition?

自古美人都是妖i 提交于 2019-12-22 04:47:23

问题


In the following when macro:

(defmacro when (condition &rest body)
  `(if ,condition (progn ,@body)))

Why is the there an at sign?


回答1:


It's very easy to see the difference by making a little experiment

> (let ((x '(1 2 3 4))) `(this is an example ,x of expansion))
(THIS IS AN EXAMPLE (1 2 3 4) OF EXPANSION)

> (let ((x '(1 2 3 4))) `(this is an example ,@x of expansion))
(THIS IS AN EXAMPLE 1 2 3 4 OF EXPANSION)

As you can see the use of ,@ will place the elements of the list directly inside in the expansion. Without you get instead the list placed in the expansion.




回答2:


The @ can also be thought of deconstructing the list and appending it to the list appears in as described in Practical Common Lisp.

`(a ,@(list 1 2) c) 

is the equivalent of:

(append (list 'a) (list 1 2) (list 'c)) 

which produces:

(a 1 2 c)


来源:https://stackoverflow.com/questions/5661875/why-is-the-sign-needed-in-this-macro-definition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!