DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(guile.info.gz) Macros

Info Catalog (guile.info.gz) Procedures with Setters (guile.info.gz) Procedures and Macros (guile.info.gz) Syntax Rules
 
 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))
 
Info Catalog (guile.info.gz) Procedures with Setters (guile.info.gz) Procedures and Macros (guile.info.gz) Syntax Rules
automatically generated byinfo2html