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) 

When the macro is ``applied'', the (unevaluated) argument S-expressions are passed to procedure. The result must be a new S-expression that can be evaluated (or expanded further). If procedure evaluates to a non-procedure, the  exn:misc:defmacro exception is raised. Macros defined with define-macro are not hygenic.

For example, the usual when macro is defined this way:

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

  Local macros are defined with let-macro:

 
  (let-macro name procedure expr  tex2html_wrap_inline8724 ) 

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

When a define-macro statement is embedded in a closure, 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.

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



PLT