[previous] [up] [next]     [contents] [index]
Next: Identifier Macros Up: Macros Previous: Macros

Defining Macros

Global macros are defined with define-macro:

  (define-macro name procedure-expr) 

When the macro is later ``applied,'' the (unevaluated) argument S-expressions are passed to the procedure obtained from procedure-expr. The result is a new S-expression that replaces the macro application expression in its context. If procedure-expr evaluates to a non-procedure, the exn:misc:defmacro exception is raised.

For example, the when macro is defined as follows:

  (define-macro when 
    (lambda (test . body)
       `(if ,test (begin ,@body)))) 

Macros defined with define-macro (at the top-level) are bound in the current namespace. Local macros are defined with let-macro:

  (let-macro name procedure-expr expr  tex2html_wrap_inline12552 ) 

This syntax is similar to define-macro, except that the macro is only effective in the body exprs. The result of a let-macro expression is the value of the expr body. Note that the environment for procedure-expr includes only global variables and it is evaluated at expansion time, not at run time. If procedure-expr evaluates to a non-procedure, the exn:misc:defmacro exception is raised.

When a define-macro statement is embedded in a sequence (like an embedded definition; see section 3.5.5), it is transformed into a let-macro expression, where the body of the closure following the define-macro statement becomes the body of the let-macro expression.

(macro? v) returns #t if v is a macro created with define-macro, #f otherwise. Note that macro? cannot be applied directly to macro identifiers, but macro values can be obtained indirectly with global-defined-value.

A define-macro expression is treated specially by compile-file (see section 15.2.4).



PLT