Relock: record the >= pin form
fc6ebe96
Archived at Sigil 0.19.0: The maintained implementation now lives in codeberg:sigil/sigil. This repository and all tags remain available for provenance, but no further standalone releases will be published.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.
(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 | Description |
|---|---|
_ | Wildcard, matches anything |
() | Empty list |
'datum | Literal value (uses equal?) |
#t / #f | Boolean literals |
(p1 . p2) | Pair: p1 matches car, p2 matches cdr |
(p1 p2 ...) | List: each element matched positionally |
var | Variable 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 |
Use (? predicate) to match values satisfying a condition:
(match x
((? positive? n) (format "positive: ~a" n))
((? negative? n) (format "negative: ~a" n))
(_ "zero"))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"))Use (= proc pat) to transform a value before matching:
(match str
((= string-length 0) "empty")
((= string-length 1) "single char")
(_ "multiple chars"))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)))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 fieldUse #{ 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));; 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))match - Main matching macromatch-lambda - Single-argument matching lambdamatch-lambda* - Multi-argument matching lambdamatch-let - Destructuring let with patternsmatch-pattern - Low-level pattern matching (used internally)sigil deps install sigil build sigil test
BSD-3-Clause
$ git clone https://codeberg.org/sigil/sigil-match
v0.9.1 — latest by version, released 1 Apr 2026
All releases — 1 tag in this repository.
One page per file, and every line has a permalink.
Relock: record the >= pin form
fc6ebe96
The last 20 commits keep a page here. Everything older lives in the clone.