On this page:
match?
as
define/ match
object
5.3.2

20 Match

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

 (require unstable/match)

The subsequent bindings were added by Carl Eastlund <cce@racket-lang.org>.

syntax

(match? val-expr pat ...)

Returns #t if the result of val-expr matches any of pat, and returns #f otherwise.

Examples:

> (match? (list 1 2 3)
    (list a b c)
    (vector x y z))

#t

> (match? (vector 1 2 3)
    (list a b c)
    (vector x y z))

#t

> (match? (+ 1 2 3)
    (list a b c)
    (vector x y z))

#f

syntax

(as ([lhs-id rhs-expr] ...) pat ...)

As a match expander, binds each lhs-id as a pattern variable with the result value of rhs-expr, and continues matching each subsequent pat.

Example:

> (match (list 1 2 3)
    [(as ([a 0]) (list b c d)) (list a b c d)])

'(0 1 2 3)

The subsequent bindings were added by Asumu Takikawa <asumu@racket-lang.org>.

syntax

(define/match (head args)
  match*-clause ...)
 
head = id
  | (head args)
     
args = arg ...
  | arg ... . rest-id
     
arg = arg-id
  | [arg-id default-expr]
  | keyword arg-id
  | keyword [arg-id default-expr]
     
match*-clause = [(pat ...+) body ...+]
  | [(pat ...+) (=> id) body ...+]
Binds id to a procedure that is defined by pattern matching clauses using match*. Each clause takes a sequence of patterns that correspond to the arguments in the function header. The arguments are ordered as they appear in the function header for matching purposes.

The function header may contain optional or keyword arguments, or may be in curried form.

Examples:

> (define/match (fact n)
    [(0) 1]
    [(n) (* n (fact (sub1 n)))])
> (fact 5)

120

> (define/match ((f x) #:y [y '(1 2 3)])
    [((regexp #rx"p+") `(,a 2 3)) a]
    [(_ _) #f])
> ((f "ape") #:y '(5 2 3))

5

> ((f "dog"))

#f

> (define/match (g x y . rst)
    [(0 0 '()) #t]
    [(5 5 '(5 5)) #t]
    [(_ _ _) #f])
> (g 0 0)

#t

> (g 5 5 5 5)

#t

> (g 1 2)

#f

syntax

(object maybe-class field-clause ...)

 
maybe-class = 
  | class-expr
     
field-clause = (field field-id maybe-pat)
     
maybe-pat = 
  | pat
A match expander that checks if the matched value is an object and contains the fields named by the field-ids. If pats are provided, the value in each field is matched to its corresponding pat. If a pat is not provided, it defaults to the name of the field.

If class-expr is provided, the match expander will also check that the supplied object is an instance of the class that the given expression evaluates to.

Examples:

(define point%
  (class object%
    (super-new)
    (init-field x y)))
> (match (make-object point% 3 5)
    [(object point% (field x) (field y))
     (sqrt (+ (* x x) (* y y)))])

5.830951894845301

> (match (make-object point% 0 0)
    [(object (field x (? zero?))
             (field y (? zero?)))
     'origin])

'origin

> (match (make-object object%)
    [(object (field x) (field y))
     'ok]
    [_ 'fail])

'fail