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 | (NOT pat ...) @match when no pat match
| (= expr pat) @match (expr value) to pat | (= expr pat) @match (expr value) to pat
| (? pred-expr pat ...) @match if (expr value) and pats | (? 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 | `qp @match quasipattern
literal ::= #t @match true literal ::= #t @match true
| #f @match false | #f @match false
| string @match equal% string | string @match equal% string
| number @match equal% number | number @match equal% number
| character @match equal% character | 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 lvp ::= pat ooo @greedily match pat instances
| pat @match pat | pat @match pat
ooo ::= *** @zero or more; *** is literal ooo ::= *** @zero or more; *** is literal

View File

@ -15,6 +15,8 @@ pat ::= id @match anything, bind identifier
| (VECTOR lvp ...) @match vector of pats | (VECTOR lvp ...) @match vector of pats
| (HASH-TABLE (pat pat) ...) @match hash table | (HASH-TABLE (pat pat) ...) @match hash table
| (HASH-TABLE (pat pat) ...+ ooo) @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 | (BOX pat) @match boxed pat
| (STRUCT struct-id (pat ...)) @match struct-id instance | (STRUCT struct-id (pat ...)) @match struct-id instance
| (REGEXP rx-expr) @match string | (REGEXP rx-expr) @match string
@ -26,8 +28,6 @@ pat ::= id @match anything, bind identifier
| (NOT pat ...) @match when no pat matches | (NOT pat ...) @match when no pat matches
| (APP expr pat) @match (expr value) to pat | (APP expr pat) @match (expr value) to pat
| (? expr pat ...) @match if (expr value) and pats | (? 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 | (QUASIQUOTE qp) @match a quasipattern
| derived-pattern @match using extension | derived-pattern @match using extension
literal ::= #t @match true literal ::= #t @match true
@ -36,6 +36,9 @@ literal ::= #t @match true
| bytes @match equal% byte string | bytes @match equal% byte string
| number @match equal% number | number @match equal% number
| char @match equal% character | 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 lvp ::= (code:line pat ooo) @greedily match pat instances
| pat @match pat | pat @match pat
qp ::= literal @match literal qp ::= literal @match literal

View File

@ -66,7 +66,7 @@
derived-pattern) derived-pattern)
(match-nonterm (symbol->string s))] (match-nonterm (symbol->string s))]
[(QUOTE LIST LIST-REST LIST-NO-ORDER VECTOR HASH-TABLE BOX STRUCT [(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" (list (string-downcase (symbol->string s))))]
[(***) [(***)
(make-element "schemesymbol" '("..."))] (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 matching on Scheme values. See also @secref["regexp"] for information
on regular-expression matching on strings, bytes, and streams. 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 ...) @defform/subs[(match val-expr clause ...)
([clause [pat expr ...+] ([clause [pat expr ...+]
@ -112,7 +112,7 @@ In more detail, patterns match as follows:
[(list 1 a ..3) a] [(list 1 a ..3) a]
[_ 'else]) [_ 'else])
(match '(1 2 3 4 5) (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]) [_ 'else])
(match '(1 (2) (2) (2) 5) (match '(1 (2) (2) (2) 5)
[(list 1 (list a) ..3 5) a] [(list 1 (list a) ..3 5) a]
@ -184,6 +184,23 @@ In more detail, patterns match as follows:
[(hash-table (key val) ...) key]) [(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. @item{@scheme[(#,(schemeidfont "box") _pat)] --- matches a boxed value.
@examples[ @examples[
@ -194,11 +211,11 @@ In more detail, patterns match as follows:
@item{@scheme[(#,(schemeidfont "struct") _struct-id (_pat ...))] --- @item{@scheme[(#,(schemeidfont "struct") _struct-id (_pat ...))] ---
matches an instance of a structure type names 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]. the corresponding @scheme[_pat].
Usually, @scheme[struct-id] is defined with Usually, @scheme[_struct-id] is defined with
@scheme[define-struct]. More generally, @scheme[struct-id] @scheme[define-struct]. More generally, @scheme[_struct-id]
must be bound to expansion-time information for a structure must be bound to expansion-time information for a structure
type (see @secref["structinfo"]), where the information type (see @secref["structinfo"]), where the information
includes at least a predicate binding and field accessor 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)]) [(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 @item{@scheme[(#,(schemeidfont "regexp") _rx-expr)] --- matches a
string that matches the regexp pattern produced by string that matches the regexp pattern produced by
@scheme[_rx-expr]; see @secref["regexp"] for more information @scheme[_rx-expr]; see @secref["regexp"] for more information
@ -311,37 +333,6 @@ In more detail, patterns match as follows:
[(list (? odd?) ...) 'yes]) [(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 @item{@scheme[(#,(schemeidfont "quasiquote") _qp)] --- introduces a
quasipattern, in which identifiers match symbols. Like the quasipattern, in which identifiers match symbols. Like the
@scheme[quasiquote] expression form, @schemeidfont{unquote} @scheme[quasiquote] expression form, @schemeidfont{unquote}