Node:Macros, Next:, Previous:Procedures with Setters, Up:Procedures and Macros

23.5 Lisp Style Macro Definitions

Macros are objects which cause the expression that they appear in to be transformed in some way before being evaluated. In expressions that are intended for macro transformation, the identifier that names the relevant macro must appear as the first element, like this:

(macro-name macro-args ...)

In Lisp-like languages, the traditional way to define macros is very similar to procedure definitions. The key differences are that the macro definition body should return a list that describes the transformed expression, and that the definition is marked as a macro definition (rather than a procedure definition) by the use of a different definition keyword: in Lisp, defmacro rather than defun, and in Scheme, define-macro rather than define.

Guile supports this style of macro definition using both defmacro and define-macro. The only difference between them is how the macro name and arguments are grouped together in the definition:

(defmacro name (args ...) body ...)

is the same as

(define-macro (name args ...) body ...)

The difference is analogous to the corresponding difference between Lisp's defun and Scheme's define.

false-if-exception, from the boot-9.scm file in the Guile distribution, is a good example of macro definition using defmacro:

(defmacro false-if-exception (expr)
  `(catch #t
          (lambda () ,expr)
          (lambda args #f)))

The effect of this definition is that expressions beginning with the identifier false-if-exception are automatically transformed into a catch expression following the macro definition specification. For example:

(false-if-exception (open-input-file "may-not-exist"))
(catch #t
       (lambda () (open-input-file "may-not-exist"))
       (lambda args #f))