Note: This is taken from the Chicken Wiki, where a more recent version could be available.
Match extension to SRFI-57.
This extension is obsolete, use matchable instead.
Taken from André van Tonders original srfi-57 reference implementation. Revised for portable syntax-case and final srfi-57 by Dale Jordan.
The match syntax is contained in the module matcher. Use
(import matcher)
to make those macros available in your code. Note that this facility is only available with the syntax-case macro system.
This version of "match" works with srfi-57 records and other Scheme constructs to provide a convenient means for deconstructing data structures without a lot of imperative code. It is based on Andre van Tonder's original srfi-57 specification, but the match functionality was withdrawn (and perhaps will appear in a later srfi).
The general syntax is:
(match expr (pattern . body) ...)
The expr is evaluated (once) and its value is compared, in order, with a series of candidate patterns and if one is successful, its corresponding body, which may be a sequence of expressions, is evaluated, returning the last expressions's value(s). Most patterns may contain variables which become bound to their corresponding portions of the value being matched.
If no pattern matches, an error is signaled. This is rather crude, and some later revision will probably enhance this behavior.
The set of patterns is easily extensible, but provides for most existing data structures.
The following table describes the patterns implemented:
Pattern | Interpretation |
---|---|
_ | Matches anything, binds nothing |
atom | Matches an eqv? atom, binds nothing |
(quote x) | Matches an equal? value, binds nothing |
identifier | Matches anything, binds identifier |
(cons x y) | Matches a pair whose components match x and y |
(vector x ...) | Matches a vector whose components match the patterns x ... |
(list x ...) | Matches a list whose components match the patterns x ... |
(list* x ... y) | Matches a list whose initial components match the patterns x ..., with the tail of the list matching y |
(and p ...) | Matches if all p ... match. (and pat x) is useful for binding x to the whole expr |
(or p ...) | Matches if any p ... match |
(= f p) | Matches if (f expr) matches p |
(? pred p ...) | Matches if (pred expr) is true and (and p ...) match. |
`p | Matches 'p |
`,p | Matches p |
`(p ...) | Matches the list of quasiquoted p ... |
`#(p ...) | Matches the vector of quasiquoted p ... |
(record-type bind ...) | Matches if expr is a srfi-57 record or scheme of type record-type. The bind ... of the pattern refer to field labels that must be defined in the record-type definition. Each bind can be a field-label or a list (field-label var). Field values from the record are bound to variables either of the same name as the field label or to the var if the second form is used. If no binds are specified, all the fields of the record are bound to variables named by their corresponding labels. A pattern for the record type alone is easily constructed from the (? pred) pattern. |
Note that the pattern matching mechanism reduces the need for record-types to have accessors defined. The srfi-57 mechanism provides anonymous accessors when they are not specified in the definition.
A pattern component of the special form
(pattern (=> fail) . body)
binds the variable (appearing here as) fail to a thunk that when called from the body, aborts the match and continues searching with the following pattern. Any variables bound by pattern are no longer lexically visible.
Some additional convenience syntax forms are also defined:
(match-lambda (pattern . body) ...)
expands to
(lambda (x) (match x (pat . body) ...))
which is useful for simple procedures that destructure a single argument.
(match-let ((pattern expr) ...) . body)
and
(match-let* ((pattern expr) ...) . body)
are similar to let and let* except they destructure their exprs according to the patterns rather than bind simple variables. The patterns have no alternatives so the expressions must be of a known type.
Copyright (c) 2005, André van Tonder All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.