[R6RS] SYNTAX-CASE

dyb at cs.indiana.edu dyb
Sat Apr 2 10:00:05 EST 2005


> > identifier-syntax
> >   identifier-syntax is an additional way to define transformers for
> >   simulated variables.  It takes one of the following two forms:
> > 
> >     (identifier-syntax template)
> > 
> >     (identifier-syntax
> >       [id template]
> >       [(set! id expr) template])
> > 
> >   note: we probably want a lower-level version of this as supported
> >         by both Chez Scheme and MzScheme.

> I think that I understand the second form but I can't figure out the
> meaning of the first one. Could you give me an example? Thanks.

Sure.  Here's a macro that creates a "read-only" definition of a variable.

> (define-syntax define-constant
    (syntax-rules ()
      [(_ x e)
       (begin
         (define t e)
         (define-syntax x (identifier-syntax t)))]))
> (define-constant f (lambda (x) (+ x 5)))
> (f 3)
8
> (map f '(1 2 3))
(6 7 8)
> (set! f 0)

[Repl(15)] Error: invalid syntax (set! f 0).
Type (debug) to enter the debugger.

Here's another example using a single identifier to refer to the car
of a pair:

> (define x (cons 3 4))
> (define-syntax foo (identifier-syntax (car x)))
> foo
3
> (set-car! x 15)
> foo
15

Here's an example of the second form, extending the preceding example.

> (define x (cons 3 4))
> (define-syntax foo
    (identifier-syntax
      [id (car x)]
      [(set! id e) (set-car! x e)]))
> foo
3
> (set! foo 12)
> foo
12
> x
(12 . 4)

Kent

PS. I realize that the syntax-case proposal is sketchy, and I will flesh
it out if we decide to include it.  In the meantime, detailed descriptions
of syntax-case and many examples are available online.  Links to some
can be found at http://www.cs.indiana.edu/chezscheme/syntax-case/.


More information about the R6RS mailing list