For Gauche 0.9.9


Next: , Previous: , Up: Macros   [Contents][Index]

5.3 Traditional macros

Special Form: define-macro name procedure
Special Form: define-macro (name . formals) body …

Defines name to be a global macro whose transformer is procedure. The second form is a shorthand notation of the following form:

(define-macro name (lambda formals body …))

When a form (name arg …) is seen by the compiler, it calls procedure with arg …. When procedure returns, the compiler inserts the returned form in place of the original form, and compile it again.

To avoid name conflict with the bindings inserted by the macro, you can use gensym, just like traditional Lisp macros (see Symbols).

(define-macro (if-let1 var test then else)
  (let1 tmp (gensym)
    `(let ((,tmp ,test))
       (if ,tmp ,then ,else))))

(macroexpand '(if-let1 v (find odd? '(2 4 6 7 8))
                 (* v v)
                 #f))
  ⇒ (let ((#:G1013 (find odd? (quote (2 4 6 7 8)))))
              (if #:G1013 (* v v) #f))

Note that gensym can’t protect name conflict with global bindings inserted by the macro. Why hygienic? discusses this issue.


Next: , Previous: , Up: Macros   [Contents][Index]