Next: , Previous: , Up: stdlib syntax-case intro   [Index]


5.12.1.3 A transformer example

Here is an example implementation of the receive syntax, giving a condensed interface to call-with-values:

(import (rnrs) (for (syntax-utilities) expand))

(define-syntax receive
  (lambda (use)
    (let ((sexp (unwrap use)))
      (let ((formals          (cadr  sexp))
            (expression       (caddr sexp))
            (body             (cdddr sexp)))
        `(,(syntax call-with-values)
              (,(syntax lambda) () ,expression)
              (,(syntax lambda) ,formals . ,body))))))

(receive (a b)
    (values 1 2)
  (write 'ciao)
  (list a b))
→ (call-with-values
        (lambda ()
          (values 1 2))
      (lambda (a b)
        (write 'ciao)
        (list a b)))

The transformer of the receive macro extracts datums and identifiers from the input form using the common list functions car, cdr, … and, for simplicity, it performs no checks to verify that the input form has the required structure (for example: it does not check that the first element after the syntactic keyword receive is a list of identifiers).

The syntax-case macro, exported by the library (rnrs syntax-case (6)), provides a way to both extract elements from a syntax object and to perform basic checks on its structure; it is usually more convenient and efficient to use syntax-case for macro–specific processing, rather than to unwrap the input form and process the result.