bazis bound to the first field and
barto the second, despite the fact that the names are reversed:
(require (lib "plt-match.ss")) (define-struct foo (bar baz)) (match (make-foo 3 4) [(struct foo (baz bar)) bar])
_) for each field in the structure, even if you only care about one of them (as above). Sometimes it would be convenient to match and decompose a structure by field name, rather than field order.
plt-match.ss, using a combination of the
(? exp pat ...)and
(app exp pat)patterns. Unfortunately, these aren't documented very thoroughly on the
(? exp pat ...)clause, we evaluate
expto a unary function and apply it to the value. If it returns a true value, then we match each remaining
patagainst the value. (The
match.sshelp-desk page does describe this pattern in detail.)
(app exp pat)clause, we evaluate
expto a unary function, apply it to the value, and match the result against
;; tested in PLT Scheme 300.3 (require (lib "plt-match.ss")) (define-struct expr (srcloc)) (define-struct (id expr) (name)) (define-struct (lam expr) (arg body)) (define-struct (app expr) (rator rand)) ;; incomplete evaluator (define (eval e r) ;; expr, environment (as alist) (match e [(? id? (app id-name name)) (cdr (assq name r))] [(? app? (app app-rand rand) (app app-rator (? lam? (app lam-arg arg) (app lam-body body)))) (eval body (cons (cons arg rand) r))]))
srclocfield for source location information to use in error messages, but the evaluator doesn't care about this. The first match clause checks to see if
id, applies the
id-nameselector to it, and binds the result to
name. Note that we have to check
id-namewill signal an error if
app. The second match clause checks to see if
app. If it is, it extracts its
app-randand its rator field with
ratorfield is then matched against a nested pattern that deconstructs the
lamvalue, again by name rather than by position. Neither match clause has to do anything with the
srclocfield, despite it being the first field in all three structures. Also, the second clause matches the fields of the
appstructure out of order, just to show that this is possible.
match.sslibrary, as it doesn't have an equivalent to the
(app exp pat)pattern form. The syntax above is quite verbose and convoluted. It'd be wonderful if someone could write a match-expander or a macro to clean this up, but my macro-fu and match-fu are, sadly, not sufficient to the task.
match.sslibrary, but instead of writing
(app exp pat), one writes
( = exp pat). (Forgive the space in front of the equals sign; it's required by the wiki's markup language.)
views.pltpackage in PLaneT: http://planet.plt-scheme.org/300/#views.plt (for PLT Scheme v350 and later only).