Next: , Previous: , Up: Scheme Syntax Extension Packages   [Contents][Index]

3.3 Macro by Example

(require 'macro-by-example)

A vanilla implementation of Macro by Example (Eugene Kohlbecker, R4RS) by Dorai Sitaram, (dorai @ cs.rice.edu) using defmacro.

3.3.1 Caveat

These macros are not referentially transparent (see Macros in Revised(4) Scheme). Lexically scoped macros (i.e., let-syntax and letrec-syntax) are not supported. In any case, the problem of referential transparency gains poignancy only when let-syntax and letrec-syntax are used. So you will not be courting large-scale disaster unless you’re using system-function names as local variables with unintuitive bindings that the macro can’t use. However, if you must have the full r4rs macro functionality, look to the more featureful (but also more expensive) versions of syntax-rules available in slib Macros That Work, Syntactic Closures, and Syntax-Case Macros.

Macro: define-syntax keyword transformer-spec

The keyword is an identifier, and the transformer-spec should be an instance of syntax-rules.

The top-level syntactic environment is extended by binding the keyword to the specified transformer.

(define-syntax let*
  (syntax-rules ()
    ((let* () body1 body2 ...)
     (let () body1 body2 ...))
    ((let* ((name1 val1) (name2 val2) ...)
       body1 body2 ...)
     (let ((name1 val1))
       (let* (( name2 val2) ...)
         body1 body2 ...)))))
Macro: syntax-rules literals syntax-rule …

literals is a list of identifiers, and each syntax-rule should be of the form

(pattern template)

where the pattern and template are as in the grammar above.

An instance of syntax-rules produces a new macro transformer by specifying a sequence of hygienic rewrite rules. A use of a macro whose keyword is associated with a transformer specified by syntax-rules is matched against the patterns contained in the syntax-rules, beginning with the leftmost syntax-rule. When a match is found, the macro use is trancribed hygienically according to the template.

Each pattern begins with the keyword for the macro. This keyword is not involved in the matching and is not considered a pattern variable or literal identifier.


Next: , Previous: , Up: Scheme Syntax Extension Packages   [Contents][Index]