There are two steps: Expand the macro first, and then evaluate the value.
Because there is a macro definition (macro body here) that cannot be evaluated during compilation, the macro parameter is not yet available. Macro parameters must be known only when the compiler sees the macro call code. Therefore, it is the only way to pass macro parameters to macro definitions and then expand the macro to evaluate the value.
It's easy to understand. It's just like C ++.
Backquote )'
In the simplest case, the function is the same as ', which means you do not evaluate a list.
`(a list of (+ 2 3) elements) ⇒ (a list of (+ 2 3) elements) '(a list of (+ 2 3) elements) ⇒ (a list of (+ 2 3) elements)
The above two expressions are equivalent.
The following macro
(defmacro inc (var) (list 'setq var (list '1+ var)))
The Simplified Method of Using backquotes is as follows:
(defmacro inc2 (var) `(setq ,var (1+ ,var)))
The special anti-quotation mark is that if the number is used in the list, it will tell the compiler how to expand the macro:
1. Evaluate the following values,
2. Replace the original expression with the value
`(a list of ,(+ 2 3) elements) ⇒ (a list of 5 elements)
Here, (+ 2 3) is evaluated as 5, and is added to this list as an element.
This usage can be applied when defining macros. Note that the difference is great.
(defmacro t-becomes-nil (variable) `(if (eq ,variable t) (setq ,variable nil)))
(macroexpand '(t-becomes-nil foo))
This macro call is equivalent:
(if (eq foo t) (setq foo nil))
If the macro definition is removed, it is equivalent:
(if (eq varialbe t) (setq variable nil))
There is no value here.
'You can use the @ symbol in the following expression to add the value of a list, for example:
(setq some-list '(2 3)) ⇒ (2 3) `(1 ,@some-list 4 ,@some-list) ⇒ (1 2 3 4 2 3)
The readability is greatly enhanced. Another method with poor readability is as follows:
(cons 1 (append some-list '(4) some-list)) ⇒ (1 2 3 4 2 3)