AtlatestRepositorysigil-match
sigil-match / treeREADME.md
1
# sigil-match3
> **Archived at Sigil 0.19.0:** The maintained implementation now lives in4
> `codeberg:sigil/sigil`. This repository and all tags remain available for5
> provenance, but no further standalone releases will be published.7
Pattern matching library for [Sigil](https://codeberg.org/sigil/sigil). Destructure and dispatch on data using patterns. Match values against shapes, bind variables, and combine patterns with guards, boolean logic, and transformations.9
## Usage11
```scheme12
(import (sigil match))14
;; Destructure a list15
(match '(1 2 3)16
((a b c) (+ a b c))) ; => 618
;; Type dispatch19
(match value20
((? number?) "it's a number")21
((? string?) "it's a string")22
(_ "something else"))24
;; Literal matching25
(match cmd26
('quit (exit))27
('help (show-help))28
(('load file) (load-file file))29
(_ (unknown-command)))30
```32
## Pattern Types34
| Pattern | Description |35
|---------|-------------|36
| `_` | Wildcard, matches anything |37
| `()` | Empty list |38
| `'datum` | Literal value (uses `equal?`) |39
| `#t` / `#f` | Boolean literals |40
| `(p1 . p2)` | Pair: `p1` matches car, `p2` matches cdr |41
| `(p1 p2 ...)` | List: each element matched positionally |42
| `var` | Variable binding |43
| `(? pred)` | Guard: matches if `(pred val)` is true |44
| `(? pred pat)` | Guarded pattern: pred and pattern must both match |45
| `(and p ...)` | All patterns must match |46
| `(or p ...)` | Any pattern matches (first wins) |47
| `(not pat)` | Negation: matches if pattern doesn't |48
| `(= proc pat)` | Transform: apply proc, then match result |49
| `($ type p ...)` | SRFI-9 record: match type tag and fields positionally |50
| `(: type k: ...)` | Sigil struct: match by keyword fields |51
| `#{ k: p ... }` | Dict: match by keyword fields |52
| `#(p ...)` | Vector: match elements positionally |54
## Guard Patterns56
Use `(? predicate)` to match values satisfying a condition:58
```scheme59
(match x60
((? positive? n) (format "positive: ~a" n))61
((? negative? n) (format "negative: ~a" n))62
(_ "zero"))63
```65
## Combining Patterns67
Use `and`, `or`, and `not` to combine patterns:69
```scheme70
;; All conditions must match71
(match n72
((and (? integer?) (? positive?)) "positive integer")73
(_ "other"))75
;; Any condition can match76
(match color77
((or 'red 'green 'blue) "primary")78
(_ "other"))80
;; Negation81
(match lst82
((not ()) "non-empty")83
(() "empty"))84
```86
## Transform Patterns88
Use `(= proc pat)` to transform a value before matching:90
```scheme91
(match str92
((= string-length 0) "empty")93
((= string-length 1) "single char")94
(_ "multiple chars"))95
```97
## Record Patterns (SRFI-9)99
Use `($ type fields ...)` to match SRFI-9 record types positionally:101
```scheme102
(define-record-type <point>103
(make-point x y) point?104
(x point-x) (y point-y))106
(match p107
(($ <point> x y) (+ x y)))108
```110
## Struct Patterns (Sigil)112
Use `(: type field: ...)` to match Sigil structs by field name:114
```scheme115
(define-struct point (x) (y))117
(match p118
((: point x: y:) (+ x y)) ; shorthand: binds x and y119
((: point x: px y: py) ...)) ; explicit binding names121
;; Match only specific fields122
(match p123
((: point y:) y)) ; only match y field124
```126
## Dict Patterns128
Use `#{ key: pat ... }` or `(dict key: ...)` to match dicts by key:130
```scheme131
(match config132
(#{ host: port: } (connect host port))133
((dict debug:) debug))135
;; Type check only136
(match val137
((dict) 'is-dict)138
(_ 'other))139
```141
## Convenience Forms143
```scheme144
;; match-lambda: anonymous function with pattern matching145
(define get-x (match-lambda ((x . _) x)))146
(get-x '(1 2 3)) ; => 1148
;; match-lambda*: match all arguments as a list149
(define add-pair150
(match-lambda* ((a b) (+ a b))))151
(add-pair 3 4) ; => 7153
;; match-let: destructuring let154
(match-let (((x y) '(1 2))155
((a . b) '(3 4 5)))156
(list x y a b))157
; => (1 2 3 (4 5))158
```160
## Exports162
- `match` - Main matching macro163
- `match-lambda` - Single-argument matching lambda164
- `match-lambda*` - Multi-argument matching lambda165
- `match-let` - Destructuring let with patterns166
- `match-pattern` - Low-level pattern matching (used internally)168
## Dependencies170
- sigil-stdlib172
## Building174
```175
sigil deps install176
sigil build177
sigil test178
```180
## License182
BSD-3-Clause