AtlatestRepositorysigil-match
sigil-match / treeREADME.md
1
# sigil-match3
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.5
## Usage7
```scheme8
(import (sigil match))10
;; Destructure a list11
(match '(1 2 3)12
((a b c) (+ a b c))) ; => 614
;; Type dispatch15
(match value16
((? number?) "it's a number")17
((? string?) "it's a string")18
(_ "something else"))20
;; Literal matching21
(match cmd22
('quit (exit))23
('help (show-help))24
(('load file) (load-file file))25
(_ (unknown-command)))26
```28
## Pattern Types30
| Pattern | Description |31
|---------|-------------|32
| `_` | Wildcard, matches anything |33
| `()` | Empty list |34
| `'datum` | Literal value (uses `equal?`) |35
| `#t` / `#f` | Boolean literals |36
| `(p1 . p2)` | Pair: `p1` matches car, `p2` matches cdr |37
| `(p1 p2 ...)` | List: each element matched positionally |38
| `var` | Variable binding |39
| `(? pred)` | Guard: matches if `(pred val)` is true |40
| `(? pred pat)` | Guarded pattern: pred and pattern must both match |41
| `(and p ...)` | All patterns must match |42
| `(or p ...)` | Any pattern matches (first wins) |43
| `(not pat)` | Negation: matches if pattern doesn't |44
| `(= proc pat)` | Transform: apply proc, then match result |45
| `($ type p ...)` | SRFI-9 record: match type tag and fields positionally |46
| `(: type k: ...)` | Sigil struct: match by keyword fields |47
| `#{ k: p ... }` | Dict: match by keyword fields |48
| `#(p ...)` | Vector: match elements positionally |50
## Guard Patterns52
Use `(? predicate)` to match values satisfying a condition:54
```scheme55
(match x56
((? positive? n) (format "positive: ~a" n))57
((? negative? n) (format "negative: ~a" n))58
(_ "zero"))59
```61
## Combining Patterns63
Use `and`, `or`, and `not` to combine patterns:65
```scheme66
;; All conditions must match67
(match n68
((and (? integer?) (? positive?)) "positive integer")69
(_ "other"))71
;; Any condition can match72
(match color73
((or 'red 'green 'blue) "primary")74
(_ "other"))76
;; Negation77
(match lst78
((not ()) "non-empty")79
(() "empty"))80
```82
## Transform Patterns84
Use `(= proc pat)` to transform a value before matching:86
```scheme87
(match str88
((= string-length 0) "empty")89
((= string-length 1) "single char")90
(_ "multiple chars"))91
```93
## Record Patterns (SRFI-9)95
Use `($ type fields ...)` to match SRFI-9 record types positionally:97
```scheme98
(define-record-type <point>99
(make-point x y) point?100
(x point-x) (y point-y))102
(match p103
(($ <point> x y) (+ x y)))104
```106
## Struct Patterns (Sigil)108
Use `(: type field: ...)` to match Sigil structs by field name:110
```scheme111
(define-struct point (x) (y))113
(match p114
((: point x: y:) (+ x y)) ; shorthand: binds x and y115
((: point x: px y: py) ...)) ; explicit binding names117
;; Match only specific fields118
(match p119
((: point y:) y)) ; only match y field120
```122
## Dict Patterns124
Use `#{ key: pat ... }` or `(dict key: ...)` to match dicts by key:126
```scheme127
(match config128
(#{ host: port: } (connect host port))129
((dict debug:) debug))131
;; Type check only132
(match val133
((dict) 'is-dict)134
(_ 'other))135
```137
## Convenience Forms139
```scheme140
;; match-lambda: anonymous function with pattern matching141
(define get-x (match-lambda ((x . _) x)))142
(get-x '(1 2 3)) ; => 1144
;; match-lambda*: match all arguments as a list145
(define add-pair146
(match-lambda* ((a b) (+ a b))))147
(add-pair 3 4) ; => 7149
;; match-let: destructuring let150
(match-let (((x y) '(1 2))151
((a . b) '(3 4 5)))152
(list x y a b))153
; => (1 2 3 (4 5))154
```156
## Exports158
- `match` - Main matching macro159
- `match-lambda` - Single-argument matching lambda160
- `match-lambda*` - Multi-argument matching lambda161
- `match-let` - Destructuring let with patterns162
- `match-pattern` - Low-level pattern matching (used internally)164
## Dependencies166
- sigil-stdlib168
## Building170
```171
sigil deps install172
sigil build173
sigil test174
```176
## License178
BSD-3-Clause