7
在下面當宏:爲什麼在這個宏定義中需要@符號?
(defmacro when (condition &rest body)
`(if ,condition (progn ,@body)))
爲什麼有一個at符號?
在下面當宏:爲什麼在這個宏定義中需要@符號?
(defmacro when (condition &rest body)
`(if ,condition (progn ,@body)))
爲什麼有一個at符號?
這很容易通過使一個小實驗
> (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)
看出區別正如你可以看到使用@
將放置列表的元素裏面直接在擴張。如果沒有,你會得到擴展名列表。
也可以認爲@
解構列表並將其附加到列表中,如Practical Common Lisp中所述。
`(a ,@(list 1 2) c)
是等價的:
(append (list 'a) (list 1 2) (list 'c))
其產生:
(a 1 2 c)