Previous: , Up: baselib definitions   [Index]


4.2.2 Syntax definitions

The define-syntax form described in this section is a ?definition used to create keyword bindings and may appear anywhere other definitions may appear.

Syntax: define-syntax ?keyword ?expression

Binds ?keyword to the value of ?expression, which must evaluate, at macro–expansion time, to a transformer. Macro transformers can be created using the syntax-rules and identifier-syntax forms.

Keyword bindings established by define-syntax are visible throughout the body in which they appear, except where shadowed by other bindings, and nowhere else, just like variable bindings established by define. All bindings established by a set of definitions, whether keyword or variable definitions, are visible within the definitions themselves.

Implementation responsibilities: The implementation should detect if the value of ?expression cannot possibly be a transformer.

Example:

(let ()
  (define even?
    (lambda (x)
      (or (= x 0) (odd? (- x 1)))))
  (define-syntax odd?
    (syntax-rules ()
      ((odd?  x) (not (even? x)))))
  (even? 10))
⇒ #t

An implication of the left–to–right processing order is that one definition can affect whether a subsequent form is also a definition.

Example:

(let ()
  (define-syntax bind-to-zero
    (syntax-rules ()
      ((bind-to-zero id) (define id 0))))
  (bind-to-zero x)
  x)
⇒ 0

The behavior is unaffected by any binding for bind-to-zero that might appear outside of the let expression.