match examples: #:when and =>

This commit is contained in:
Matthew Butterick 2018-08-20 11:12:58 -07:00 committed by Matthew Butterick
parent 2d09e121c5
commit 70f7cf99ae

View File

@ -39,6 +39,19 @@ matched before calling the failure procedure, otherwise the behavior
of matching is unpredictable. See also @racket[failure-cont], which is
a lower-level mechanism achieving the same ends.
@examples[
#:eval match-eval
(define (m x)
(match x
[(list a b c)
#:when (= 6 (+ a b c))
'sum-is-six]
[(list a b c) 'sum-is-not-six]))
(m '(1 2 3))
(m '(2 3 4))
]
An optional @racket[(=> id)] between a @racket[pat] and the
@racket[body]s is bound to a @deftech{failure procedure} of zero
arguments. If this procedure is invoked, it escapes back to the
@ -47,6 +60,24 @@ the pattern had failed to match. The @racket[body]s must not mutate
the object being matched before calling the failure procedure,
otherwise the behavior of matching is unpredictable.
@examples[
#:eval match-eval
(define (m x)
(match x
[(list a b c)
(=> exit)
(f x exit)]
[(list a b c) 'sum-is-not-six]))
(define (f x exit)
(if (= 6 (apply + x))
'sum-is-six
(exit)))
(m '(1 2 3))
(m '(2 3 4))
]
The grammar of @racket[pat] is as follows, where non-italicized
identifiers are recognized symbolically (i.e., not by binding).