From 70f7cf99ae32fdb2d69caad3c35a054addd08bce Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 20 Aug 2018 11:12:58 -0700 Subject: [PATCH] `match` examples: `#:when` and `=>` --- .../scribblings/reference/match.scrbl | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkgs/racket-doc/scribblings/reference/match.scrbl b/pkgs/racket-doc/scribblings/reference/match.scrbl index 872e9ce289..b9a242ee8f 100644 --- a/pkgs/racket-doc/scribblings/reference/match.scrbl +++ b/pkgs/racket-doc/scribblings/reference/match.scrbl @@ -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).