sigildocs

(sigil match)

(sigil match) - Pattern Matching

Destructure and dispatch on data using patterns. Match values against shapes, bind variables, and combine patterns with guards, boolean logic, and transformations.

Basic Usage

(import (sigil match))

;; Destructure a list
(match '(1 2 3)
  ((a b c) (+ a b c)))  ; => 6

;; Type dispatch
(match value
  ((? number?) "it's a number")
  ((? string?) "it's a string")
  (_ "something else"))

Pattern Types

PatternDescription
_Wildcard, matches anything
()Empty list
'datumLiteral value (uses equal?)
#t / #fBoolean literals
(p1 . p2)Pair: p1 matches car, p2 matches cdr
(p1 p2 ...)List: each element matched positionally
varVariable binding
(? pred)Guard: matches if (pred val) is true
(? pred pat)Guarded pattern: pred and pattern must both match
(and p ...)All patterns must match
(or p ...)Any pattern matches (first wins)
(not pat)Negation: matches if pattern doesn't
(= proc pat)Transform: apply proc, then match result
($ type p ...)SRFI-9 record: match type tag and fields positionally
(: type k: ...)Sigil struct: match by keyword fields
#{ k: p ... }Dict: match by keyword fields
#(p ...)Vector: match elements positionally

Guard Patterns

Use (? predicate) to match values satisfying a condition:

(match x
  ((? positive? n) (format "positive: ~a" n))
  ((? negative? n) (format "negative: ~a" n))
  (_ "zero"))

Combining Patterns

Use and, or, and not to combine patterns:

;; All conditions must match
(match n
  ((and (? integer?) (? positive?)) "positive integer")
  (_ "other"))

;; Any condition can match
(match color
  ((or 'red 'green 'blue) "primary")
  (_ "other"))

;; Negation
(match lst
  ((not ()) "non-empty")
  (() "empty"))

Transform Patterns

Use (= proc pat) to transform a value before matching:

(match str
  ((= string-length 0) "empty")
  ((= string-length 1) "single char")
  (_ "multiple chars"))

Record Patterns (SRFI-9)

Use ($ type fields ...) to match SRFI-9 record types positionally:

(define-record-type <point>
  (make-point x y) point?
  (x point-x) (y point-y))

(match p
  (($ <point> x y) (+ x y)))

Struct Patterns (Sigil)

Use (: type field: ...) to match Sigil structs by field name:

(define-struct point (x) (y))

(match p
  ((: point x: y:) (+ x y))        ; shorthand: binds x and y
  ((: point x: px y: py) ...))     ; explicit binding names

;; Match only specific fields
(match p
  ((: point y:) y))                ; only match y field

Dict Patterns

Use #{ key: pat ... } or (dict key: ...) to match dicts by key:

(match config
  (#{ host: port: } (connect host port))  ; explicit bindings required
  ((dict debug:) debug))                   ; shorthand with (dict ...)

;; Type check only
(match val
  ((dict) 'is-dict)
  (_ 'other))

Exports

matchvariable

(No description)

match-lambdavariable

(No description)

(No description)

match-letvariable

(No description)

(No description)

(No description)

(No description)

(No description)

(No description)

(No description)

%match-dictvariable

(No description)