On this page:
match?
define-struct-pattern
as
$

6 Pattern Matching

 (require (planet cce/scheme:7:0/match))

This module provides tools for pattern matching with match.

(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

(define-struct-pattern pat-id struct-id)
Defines pat-id as a match expander that takes one pattern argument per field of the structure described by struct-id. The resulting match expander recognizes instances of the structure and matches their fields against the corresponding patterns.

Examples:

  (define-struct pair [a b] #:transparent)
  > (define-struct-pattern both pair)
  > (match (make-pair 'left 'right)
      [(both a b) (list a b)])

  '(left right)

(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)

($ struct-id expr ...)
($ struct-id pat ...)
As an expression, constructs an instance of the structure described by struct-id with fields specified by each expr.

As a match expander, matches instances of the structure described by struct-id with fields matched by each pat.

Examples:

  (define-struct pair [a b] #:transparent)
  > ($ pair 1 2)

  (pair 1 2)

  > (match ($ pair 1 2)
      [($ pair a b) (list a b)])

  '(1 2)