Update match documentation to new implementation:

- Mention cons and mcons pattern.
 - Describe greedyness.
 - Mention additional literals in grammar.

svn: r9095
This commit is contained in:
Sam Tobin-Hochstadt 2008-03-27 17:35:11 +00:00
parent 61ea615004
commit aa40a654b0
4 changed files with 37 additions and 41 deletions

View File

@ -18,14 +18,16 @@ pat ::= id @match anything, bind identifier
| (NOT pat ...) @match when no pat match
| (= expr pat) @match (expr value) to pat
| (? pred-expr pat ...) @match if (expr value) and pats
| (set! identifier) @match anything, bind as setter
| (get! identifier) @match anything, bind as getter
| `qp @match quasipattern
literal ::= #t @match true
| #f @match false
| string @match equal% string
| number @match equal% number
| character @match equal% character
| bytes @match equal% byte string
| keyword @match equal% keyword
| regexp literal @match equal% regexp literal
| pregexp literal @match equal% pregexp literal
lvp ::= pat ooo @greedily match pat instances
| pat @match pat
ooo ::= *** @zero or more; *** is literal

View File

@ -15,6 +15,8 @@ pat ::= id @match anything, bind identifier
| (VECTOR lvp ...) @match vector of pats
| (HASH-TABLE (pat pat) ...) @match hash table
| (HASH-TABLE (pat pat) ...+ ooo) @match hash table
| (CONS pat pat) @match pair of pats
| (MCONS pat pat) @match mutable pair of pats
| (BOX pat) @match boxed pat
| (STRUCT struct-id (pat ...)) @match struct-id instance
| (REGEXP rx-expr) @match string
@ -26,8 +28,6 @@ pat ::= id @match anything, bind identifier
| (NOT pat ...) @match when no pat matches
| (APP expr pat) @match (expr value) to pat
| (? expr pat ...) @match if (expr value) and pats
| (set! id) @match anything, bind as setter
| (get! id) @match anything, bind as getter
| (QUASIQUOTE qp) @match a quasipattern
| derived-pattern @match using extension
literal ::= #t @match true
@ -36,6 +36,9 @@ literal ::= #t @match true
| bytes @match equal% byte string
| number @match equal% number
| char @match equal% character
| keyword @match equal% keyword
| regexp literal @match equal% regexp literal
| pregexp literal @match equal% pregexp literal
lvp ::= (code:line pat ooo) @greedily match pat instances
| pat @match pat
qp ::= literal @match literal

View File

@ -66,7 +66,7 @@
derived-pattern)
(match-nonterm (symbol->string s))]
[(QUOTE LIST LIST-REST LIST-NO-ORDER VECTOR HASH-TABLE BOX STRUCT
REGEXP PREGEXP AND OR NOT APP ? get! set! QUASIQUOTE)
REGEXP PREGEXP AND OR NOT APP ? QUASIQUOTE CONS MCONS)
(make-element "schemesymbol" (list (string-downcase (symbol->string s))))]
[(***)
(make-element "schemesymbol" '("..."))]

View File

@ -12,7 +12,7 @@ The @scheme[match] form and related forms support general pattern
matching on Scheme values. See also @secref["regexp"] for information
on regular-expression matching on strings, bytes, and streams.
@note-lib[scheme/match #:use-sources (mzlib/plt-match)]
@note-lib[scheme/match #:use-sources (scheme/match)]
@defform/subs[(match val-expr clause ...)
([clause [pat expr ...+]
@ -112,7 +112,7 @@ In more detail, patterns match as follows:
[(list 1 a ..3) a]
[_ 'else])
(match '(1 2 3 4 5)
[(list 1 a ..3 5) a] (code:comment #, @t{greedy matching to @scheme[a] leaves no @scheme[5]})
[(list 1 a ..3 5) a]
[_ 'else])
(match '(1 (2) (2) (2) 5)
[(list 1 (list a) ..3 5) a]
@ -184,6 +184,23 @@ In more detail, patterns match as follows:
[(hash-table (key val) ...) key])
]}
@item{@scheme[(#,(schemeidfont "cons") _pat1 _pat2)] --- matches a pair value.
@examples[
#:eval match-eval
(match (cons 1 2)
[(cons a b) (+ a b)])
]}
@item{@scheme[(#,(schemeidfont "mcons") _pat1 _pat2)] --- matches a mutable pair value.
@examples[
#:eval match-eval
(match (mcons 1 2)
[(cons a b) 'immutable]
[(mcons a b) 'mutable])
]}
@item{@scheme[(#,(schemeidfont "box") _pat)] --- matches a boxed value.
@examples[
@ -194,11 +211,11 @@ In more detail, patterns match as follows:
@item{@scheme[(#,(schemeidfont "struct") _struct-id (_pat ...))] ---
matches an instance of a structure type names
@scheme[_struct-id], where each field int he instance matches
@scheme[_struct-id], where each field in the instance matches
the corresponding @scheme[_pat].
Usually, @scheme[struct-id] is defined with
@scheme[define-struct]. More generally, @scheme[struct-id]
Usually, @scheme[_struct-id] is defined with
@scheme[define-struct]. More generally, @scheme[_struct-id]
must be bound to expansion-time information for a structure
type (see @secref["structinfo"]), where the information
includes at least a predicate binding and field accessor
@ -215,6 +232,11 @@ In more detail, patterns match as follows:
[(struct tree (a (struct tree (b _ _)) _)) (list a b)])
]}
@item{@scheme[(#,(schemeidfont "struct") _struct-id _)] ---
matches any instance of @scheme[_struct-id], without regard to
contents of the fields of the instance.
}
@item{@scheme[(#,(schemeidfont "regexp") _rx-expr)] --- matches a
string that matches the regexp pattern produced by
@scheme[_rx-expr]; see @secref["regexp"] for more information
@ -311,37 +333,6 @@ In more detail, patterns match as follows:
[(list (? odd?) ...) 'yes])
]}
@item{@scheme[(#,(schemeidfont "set!") _id)] --- matches anything,
and binds @scheme[_id] to a procedure that takes one argument
and mutates the matched value to install the given one in the
matched position. This form can be used only within a
@schemeidfont{vector}, @schemeidfont{box}, or
@schemeidfont{struct} pattern.
@examples[
#:eval match-eval
(define v (vector 1 2 3))
(match v
[(vector _ (set! s!) _) (s! 0)])
v
]}
@item{@scheme[(#,(schemeidfont "get!") _id)] --- matches anything, and
binds @scheme[_id] to a thunk that extracts the matched
position from the matched value, which is useful when the
matched position is mutable. This form can be used
only in the same places as the @schemeidfont{set!} pattern.
@examples[
#:eval match-eval
(define v (vector 1 2 3))
(define g
(match v
[(vector _ (get! g) _) g]))
(vector-set! v 1 0)
(g)
]}
@item{@scheme[(#,(schemeidfont "quasiquote") _qp)] --- introduces a
quasipattern, in which identifiers match symbols. Like the
@scheme[quasiquote] expression form, @schemeidfont{unquote}