Here is a linear matcher. It doesn't optimize by sharing work between
rows - but the pattern language is extensible. The main use of this is
to add record-matching:
https://github.com/rain-1/chez-match/blob/master/el-match.scm
(import (rename (chezscheme)
(define-record %define-record))
(match))
;; redefine define-record so that we get nicer printing
(define-syntax define-record
(syntax-rules ()
((define-record <name> (<field> ...))
(begin (%define-record <name> (<field> ...))
(define-record-matcher <name> (<field> ...))
(record-reader '<name> (type-descriptor <name>))))))
In order to use it you do have to call some function from inside the
module
<
https://github.com/cisco/ChezScheme/issues/116>. Doing this seems to
work inside a module:
(define-syntax %foo
(begin (foo)
(lambda (stx) stx)))
Here's an example of use:
> (import (match) (modified-define-record))
> (define-record kons (kar kdr))
> (match (make-kons 1 2) ((kons x y) (list y x)))
(2 1)
> (match (make-kons 1 2) ((kons '1 y) (list y)))
Exception in match-pats: no expander for pattern with irritant (quote 1)
Type (debug) to enter the debugger.
> (foo)
#t
> (match (make-kons 1 2) ((kons '1 y) (list y)))
(2)
I welcome any criticism or review of the code.