Why is the @ sign needed in this macro definition?

2019-04-23 23:18发布

In the following when macro:

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

Why is the there an at sign?

标签: macros lisp
2条回答
男人必须洒脱
2楼-- · 2019-04-23 23:36

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)
查看更多
混吃等死
3楼-- · 2019-04-23 23:40

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.

查看更多
登录 后发表回答