[previous] [up] [next]     [index]
Next: zodiac:make-match&env Up: Pattern Matching Previous: Pattern Matching

Examples

The source for match-and-rewrite is presented first:

(define match-and-rewrite
  (lambda (expr rewriter out kwd env)
    (let ((p-env (match-against rewriter expr env)))
      (and p-env
           (pexpand out p-env kwd))))))

This assumes that a compiled pattern has already been generated for use as the rewriter argument. A typical use might be:

(let* ((kwd '(let))
       (in-pattern '(let ((v e) ...) b))
       (out-pattern '((lambda (v ...) b) e ...))
       (m\&e (make-match\&env in-pattern-1 kwd)))
  (lambda (expr env)
    (or (match-and-rewrite expr m\&e out-pattern kwd env)
        (static-error expr "Malformed let"))))

This implements the let macro used by many Scheme implementations. Note that the compiled pattern, bound to m&e, is created outside the procedure representing the let macro.

  (let* ((kwd '(lambda))
         (in-pattern '(lambda args body))
         (m&e (make-match&env in-pattern kwd)))
    (lambda (expr env attributes vocab)
      (cond
        ((match-against m&e expr env)
         =>
         (lambda (p-env)
           (let ((args (pexpand 'args p-env kwd))
                 (body (pexpand 'body p-env kwd)))
             (make-lambda-form args body))))
        (else
         (static-error expr "Malformed lambda body")))))

In this example, a simplified version of the Scheme lambda expression is shown. Note that there is no checking done to ensure that args does indeed match against a well-formed argument list. After the pattern variables are expanded, the results are passed to the procedure make-lambda-form, which may represent an abstract syntax constructor.

Procedures



PLT