Elisp programming 13th: Macro 2

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.