On this page:
match
8.1 Additional Matching Forms
match*
match/ values
match-lambda
match-lambda*
match-lambda**
match-let
match-let*
match-let-values
match-let*-values
match-letrec
match-define
match-define-values
exn: misc: match?
8.2 Extending match
define-match-expander
prop: match-expander
prop: legacy-match-expander
match-equality-test
match/ derived
match*/ derived
8.3 Library Extensions
==
struct*

8 Pattern Matching

+Pattern Matching in The Racket Guide introduces pattern matching.

The match form and related forms support general pattern matching on Racket values. See also Regular Expressions for information on regular-expression matching on strings, bytes, and streams.

The bindings documented in this section are provided by the racket/match and racket libraries, but not racket/base.

syntax

(match val-expr clause ...)

 
clause = [pat body ...+]
  | [pat (=> id) body ...+]
Finds the first pat that matches the result of val-expr, and evaluates the corresponding bodys with bindings introduced by pat (if any). The last body in the matching clause is evaluated in tail position with respect to the match expression.

To find a match, the clauses are tried in order. If no clause matches, then the exn:misc:match? exception is raised.

An optional (=> id) between a pat and the bodys is bound to a failure procedure of zero arguments. If this procedure is invoked, it escapes back to the pattern matching expression, and resumes the matching process as if the pattern had failed to match. The bodys must not mutate the object being matched before calling the failure procedure, otherwise the behavior of matching is unpredictable.

The grammar of pat is as follows, where non-italicized identifiers are recognized symbolically (i.e., not by binding).

pat

 

::=

 

id

 

match anything, bind identifier

 

 

 |

 

(var id)

 

match anything, bind identifier

 

 

 |

 

_

 

match anything

 

 

 |

 

literal

 

match literal

 

 

 |

 

(quote datum)

 

match equal? value

 

 

 |

 

(list lvp ...)

 

match sequence of lvps

 

 

 |

 

(list-rest lvp ... pat)

 

match lvps consed onto a pat

 

 

 |

 

(list-no-order pat ...)

 

match pats in any order

 

 

 |

 

(list-no-order pat ... lvp)

 

match pats in any order

 

 

 |

 

(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-id pat ...)

 

match struct-id instance

 

 

 |

 

(struct struct-id (pat ...))

 

match struct-id instance

 

 

 |

 

(regexp rx-expr)

 

match string

 

 

 |

 

(regexp rx-expr pat)

 

match string, result with pat

 

 

 |

 

(pregexp px-expr)

 

match string

 

 

 |

 

(pregexp px-expr pat)

 

match string, result with pat

 

 

 |

 

(and pat ...)

 

match when all pats match

 

 

 |

 

(or pat ...)

 

match when any pat match

 

 

 |

 

(not pat ...)

 

match when no pat matches

 

 

 |

 

(app expr pat)

 

match (expr value) to pat

 

 

 |

 

(? expr pat ...)

 

match if (expr value) and pats

 

 

 |

 

(quasiquote qp)

 

match a quasipattern

 

 

 |

 

derived-pattern

 

match using extension

literal

 

::=

 

#t

 

match true

 

 

 |

 

#f

 

match false

 

 

 |

 

string

 

match equal? string

 

 

 |

 

bytes

 

match equal? byte string

 

 

 |

 

number

 

match equal? number

 

 

 |

 

char

 

match equal? character

 

 

 |

 

keyword

 

match equal? keyword

 

 

 |

 

regexp

 

match equal? regexp literal

 

 

 |

 

pregexp

 

match equal? pregexp literal

lvp

 

::=

 

pat ooo

 

greedily match pat instances

 

 

 |

 

pat

 

match pat

qp

 

::=

 

literal

 

match literal

 

 

 |

 

id

 

match symbol

 

 

 |

 

(qp ...)

 

match sequences of qps

 

 

 |

 

(qp ... . qp)

 

match qps ending qp

 

 

 |

 

(qp ooo . qp)

 

match qps beginning with repeated qp

 

 

 |

 

#(qp ...)

 

match vector of qps

 

 

 |

 

#&qp

 

match boxed qp

 

 

 |

 

,pat

 

match pat

 

 

 |

 

,@(list lvp ...)

 

match lvps, spliced

 

 

 |

 

,@(list-rest lvp ... pat)

 

match lvps plus pat, spliced

 

 

 |

 

,@'qp

 

match list-matching qp, spliced

ooo

 

::=

 

...

 

zero or more; ... is literal

 

 

 |

 

___

 

zero or more

 

 

 |

 

..k

 

k or more

 

 

 |

 

__k

 

k or more

In more detail, patterns match as follows:

Note that the matching process may destructure the input multiple times, and may evaluate expressions embedded in patterns such as (app expr pat) in arbitrary order, or multiple times. Therefore, such expressions must be safe to call multiple times, or in an order other than they appear in the original program.

8.1 Additional Matching Forms

syntax

(match* (val-expr ...+) clause* ...)

 
clause* = [(pat ...+) body ...+]
  | [(pat ...+) (=> id) body ...+]
Matches a sequence of values against each clause in order, matching only when all patterns in a clause match. Each clause must have the same number of patterns as the number of val-exprs.

Example:

> (match* (1 2 3)
   [(_ (? number?) x) (add1 x)])

4

syntax

(match/values expr clause clause ...)

If expr evaluates to n values, then match all n values against the patterns in clause .... Each clause must contain exactly n patterns. At least one clause is required to determine how many values to expect from expr.

syntax

(match-lambda clause ...)

Equivalent to (lambda (id) (match id clause ...)).

syntax

(match-lambda* clause ...)

Equivalent to (lambda lst (match lst clause ...)).

syntax

(match-lambda** clause* ...)

Equivalent to (lambda (args ...) (match* (args ...) clause* ...)), where the number of args ... is computed from the number of patterns appearing in each of the clause*.

syntax

(match-let ([pat expr] ...) body ...+)

Generalizes let to support pattern bindings. Each expr is matched against its corresponding pat (the match must succeed), and the bindings that pat introduces are visible in the bodys.

Example:

> (match-let ([(list a b) '(1 2)]
              [(vector x ...) #(1 2 3 4)])
    (list b a x))

'(2 1 (1 2 3 4))

syntax

(match-let* ([pat expr] ...) body ...+)

Like match-let, but generalizes let*, so that the bindings of each pat are available in each subsequent expr.

Example:

> (match-let* ([(list a b) '(#(1 2 3 4) 2)]
               [(vector x ...) a])
    x)

'(1 2 3 4)

syntax

(match-let-values ([(pat ...) expr] ...) body ...+)

Like match-let, but generalizes let-values.

syntax

(match-let*-values ([(pat ...) expr] ...) body ...+)

Like match-let*, but generalizes let*-values.

syntax

(match-letrec ([pat expr] ...) body ...+)

Like match-let, but generalizes letrec.

syntax

(match-define pat expr)

Defines the names bound by pat to the values produced by matching against the result of expr.

Examples:

> (match-define (list a b) '(1 2))
> b

2

syntax

(match-define-values (pat pats ...) expr)

Like match-define but for when expr produces multiple values. Like match/values, it requires at least one pattern to determine the number of values to expect.

Examples:

> (match-define-values (a b) (values 1 2))
> b

2

procedure

(exn:misc:match? v)  boolean?

  v : any/c
A predicate for the exception raised in the case of a match failure.

8.2 Extending match

syntax

(define-match-expander id proc-expr)

(define-match-expander id proc-expr proc-expr)
Binds id to a match expander.

The first proc-expr sub-expression must evaluate to a transformer that produces a pat for match. Whenever id appears as the beginning of a pattern, this transformer is given, at expansion time, a syntax object corresponding to the entire pattern (including id). The pattern is replaced with the result of the transformer.

A transformer produced by a second proc-expr sub-expression is used when id is used in an expression context. Using the second proc-expr, id can be given meaning both inside and outside patterns.

For example, to extend the pattern matcher and destructure syntax lists,
(define-match-expander syntax-list
  (lambda (stx)
    (syntax-case stx ()
      [(_ elts ...)
       #'(? (lambda (x) (and (syntax? x)
                             (list? (syntax->list x))))
            (app syntax->list (list elts ...)))])))
(define (make-keyword-predicate keyword)
  (lambda (stx)
    (and (identifier? stx)
         (free-identifier=? stx keyword))))
(define or-keyword? (make-keyword-predicate #'or))
(define and-keyword? (make-keyword-predicate #'and))

 

> (match #'(or 3 4)
    [(syntax-list (? or-keyword?) b c)
     (list "OOORRR!" b c)]
    [(syntax-list (? and-keyword?) b c)
     (list "AAANND!" b c)])

'("OOORRR!" #<syntax:47:0 3> #<syntax:47:0 4>)

> (match #'(and 5 6)
    [(syntax-list (? or-keyword?) b c)
     (list "OOORRR!" b c)]
    [(syntax-list (? and-keyword?) b c)
     (list "AAANND!" b c)])

'("AAANND!" #<syntax:48:0 5> #<syntax:48:0 6>)

A structure type property to identify structure types that act as match expanders like the ones created by define-match-expander.

The property value must be an exact non-negative integer or a procedure of one or two arguments. In the former case, the integer designates a field within the structure that should contain a procedure; the integer must be between 0 (inclusive) and the number of non-automatic fields in the structure type (exclusive, not counting supertype fields), and the designated field must also be specified as immutable.

If the property value is a procedure of one argument, then the procedure serves as the transformer for match expansion. If the property value is a procedure of two arguments, then the first argument is the structure whose type has prop:match-expander property, and the second argument is a syntax object as for a match expander..

If the property value is a assignment transformer, then the wrapped procedure is extracted with set!-transformer-procedure before it is called.

Like prop:match-expander, but for the legacy match syntax.

parameter

(match-equality-test)  (any/c any/c . -> . any)

(match-equality-test comp-proc)  void?
  comp-proc : (any/c any/c . -> . any)
A parameter that determines the comparison procedure used to check whether multiple uses of an identifier match the “same” value. The default is equal?.

syntax

(match/derived val-expr original-datum clause ...)

syntax

(match*/derived (val-expr ...) original-datum clause* ...)

Like match and match* respectively, but includes a sub-expression to be used as the source for all syntax errors within the form. For example, match-lambda expands to match/derived so that errors in the body of the form are reported in terms of match-lambda instead of match.

8.3 Library Extensions

syntax

(== val comparator)

(== val)
A match expander which checks if the matched value is the same as val when compared by comparator. If comparator is not provided, it defaults to equal?.

Examples:

> (match (list 1 2 3)
    [(== (list 1 2 3)) 'yes]
    [_ 'no])

'yes

> (match (list 1 2 3)
    [(== (list 1 2 3) eq?) 'yes]
    [_ 'no])

'no

> (match (list 1 2 3)
    [(list 1 2 (== 3 =)) 'yes]
    [_ 'no])

'yes

syntax

(struct* struct-id ([field pat] ...))

A match pattern form that matches an instance of a structure type named struct-id, where the field field in the instance matches the corresponding pat.

Any field of struct-id may be omitted, and such fields can occur in any order.

Examples:

(define-struct tree (val left right))
> (match (make-tree 0 (make-tree 1 #f #f) #f)
    [(struct* tree ([val a]
                    [left (struct* tree ([right #f] [val b]))]))
     (list a b)])

'(0 1)