LISP Tutorial on LISP Macros

macros allow you to extend the syntax of standard lisp.

technically, a macro is a function that takes an s-expression as arguments and returns a lisp form, which is then evaluated.

defining a macro

in lisp, a named macro is defined using another macro named defmacro. syntax for defining a macro is −

(defmacro macro-name (parameter-list))
"optional documentation string."
body-form

the macro definition consists of the name of the macro, a parameter list, an optional documentation string, and a body of lisp expressions that defines the job to be performed by the macro.

example

let us write a simple macro named setto10, which will take a number and set its value to 10.

create new source code file named main.lisp and type the following code in it.

(defmacro setto10(num)
(setq num 10)(print num))
(setq x 25)
(print x)
(setto10 x)

when you click the execute button, or type ctrl+e, lisp executes it immediately and the result returned is −

25
10