On this page:
Version: 5.2.1

3 Datum Pattern Matching

The syntax/datum library provides forms that implement the pattern and template language of syntax-case, but for matching and constructing datum values instead of syntax.

For most pattern-matching purposes, racket/match is a better choice than syntax/datum. The syntax/datum library is useful mainly for its template support (i.e., datum) and, to a lesser extent, its direct correspondence to syntax-case patterns.

(datum-case datum-expr (literal-id ...)
  clause ...)
(datum template)
Like syntax-case and syntax, but datum-expr in datum-case should produce a datum (i.e., plain S-expression) instead of a syntax object to be matched in clauses, and datum similarly produces a datum. Pattern variables bound in each clause of datum-case are accessible via datum instead of syntax. When a literal-id appears in a clause’s pattern, it matches the corresponding symbol (using eq?).

Using datum-case and datum is essentially equivalent to converting the input to syntax-case using datum->syntax and then wrapping each use of syntax with syntax->datum, but datum-case and datum to not create intermediate syntax objects.


> (datum-case '(1 "x" -> y) (->)
    [(a ... -> b) (datum (b (+ a) ...))])

'(y (+ 1) (+ "x"))

(with-datum ([pattern datum-expr] ...)
  body ...+)
Analogous to with-syntax, but for datum-case and datum instead of syntax-case and syntax.


> (with-datum ([(a ...) '(1 2 3)]
               [(b ...) '("x" "y" "z")])
    (datum ((a b) ...)))

'((1 "x") (2 "y") (3 "z"))

(quasidatum template)
(undatum expr)
(undatum-splicing expr)


> (with-datum ([(a ...) '(1 2 3)])
    (quasidatum ((undatum (- 1 1)) a ... (undatum (+ 2 2)))))

'(0 1 2 3 4)