Contents    Page-10    Prev    Next    Page+10    Index   

Macros

A macro is a function from code to code, usually turning a short piece of code into a longer code sequence.

Lisp macros produce Lisp code as output; this code is executed or compiled.


(defn neq [x y] (not (= x y)))  ; like !=

(defmacro neq [x y] (list 'not (list '= x y)))

> (neq 2 3)
true
> (macroexpand '(neq 2 3))
(not (= 2 3))

If a macro uses its own variables, it is important to generate new ones with gensym to avoid variable capture or name conflicts with calling code.


> (gensym 'foo)
foo2382