Macros

Overview

  • Macros generate code

Example

(defmacro thread (&rest body)
  `(sb-thread:make-thread (lambda () (progn ,@body)) :name ,(symbol-name (gensym))))
  • ` defines partial evaluation (only code prefixed by , will get executed)

  • the @ before body handles the variable as a list

  • gensym generates a random symbol name

  • this macro can be used like

(thread (print "FOO") (print "BAR"))

Debugging

  • macroexpand resolves the real code of a macro

  • the macro must be prefixed by ‘ otherwise it gets executed

(macroexpand '(mymacro "param"))