Version0.9.1Verifiednot yet verifiedLicenseBSD-3-Clause

sigil-match

Pattern matching library for Sigil

Report a bug or suggest a feature
Readme

sigil-match

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

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

;; Literal matching
(match cmd
  ('quit (exit))
  ('help (show-help))
  (('load file) (load-file file))
  (_ (unknown-command)))

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"))
Read the rest

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))
  ((dict debug:) debug))

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

Convenience Forms

;; match-lambda: anonymous function with pattern matching
(define get-x (match-lambda ((x . _) x)))
(get-x '(1 2 3))  ; => 1

;; match-lambda*: match all arguments as a list
(define add-pair
  (match-lambda* ((a b) (+ a b))))
(add-pair 3 4)  ; => 7

;; match-let: destructuring let
(match-let (((x y) '(1 2))
            ((a . b) '(3 4 5)))
  (list x y a b))
; => (1 2 3 (4 5))

Exports

  • match - Main matching macro
  • match-lambda - Single-argument matching lambda
  • match-lambda* - Multi-argument matching lambda
  • match-let - Destructuring let with patterns
  • match-pattern - Low-level pattern matching (used internally)

Dependencies

  • sigil-stdlib

Building

sigil deps install
sigil build
sigil test

License

BSD-3-Clause

Clone

$ git clone https://codeberg.org/sigil/sigil-match

Releases

v0.9.1 — latest by version, released 1 Apr 2026

All releases — 1 tag in this repository.

Source

Browse the latest source

One page per file, and every line has a permalink.

Recent commitsAtom

The last 20 commits keep a page here. Everything older lives in the clone.