6 Pattern Matching
(require (planet cce/scheme:7:0/match)) |
This module provides tools for pattern matching with match.
(match? val-expr pat ...) |
Returns #t if the result of val-expr matches any of
pat, and returns #f otherwise.
Examples: | |||
| |||
#t | |||
| |||
#t | |||
| |||
#f |
(define-struct-pattern pat-id struct-id) |
Defines pat-id as a match expander that takes one pattern argument per
field of the structure described by struct-id. The resulting match
expander recognizes instances of the structure and matches their fields against
the corresponding patterns.
Examples: | ||
| ||
> (define-struct-pattern both pair) | ||
| ||
'(left right) |
(as ([lhs-id rhs-expr] ...) pat ...) |
As a match expander, binds each lhs-id as a pattern variable with the
result value of rhs-expr, and continues matching each subsequent
pat.
Example: | ||
| ||
'(0 1 2 3) |
As an expression, constructs an instance of the structure described by
struct-id with fields specified by each expr.
As a match expander, matches instances of the structure described by struct-id with fields matched by each pat.
Examples: | ||
| ||
> ($ pair 1 2) | ||
(pair 1 2) | ||
| ||
'(1 2) |