Simple pattern-matching for Common Lisp
(pattern-case KEYFORM (PATTERN FORM*)*)
(defun factorial (n)
(pattern-case n
(0 1)
(n (* n (factorial (1- n))))))
(defun kth (k list)
(pattern-case (list k list)
((_ nil) (error "much k. little list. such fail. wow."))
((0 (x . _)) x)
((k (_ . xs)) (kth (1- k) xs))))
- Literal values are typechecked; symbols aren’t.
- By convention
_
is an ignored term and remains unbound; multiple_
may appear inPATTERN
.
Pattern | Matches |
---|---|
t | generalized boolean (i.e. non-null) |
nil | itself |
:keyword | itself |
_ | anything |
other symbol | anything; bind to expression |
other atom | itself |
Pattern | Expression | Match? | Binds |
---|---|---|---|
0 | 0 | Y | |
0 | 0.0 | N | |
(n . ns) | (1 2 3) | Y | n => 1 ns => (2 3) |
(n . ns) | (1) | Y | n => 1 ns => () |
(a b) | (BATMAN ROBIN) | Y | a => BATMAN b => ROBIN |
(a b) | (LARRY MOE CURLY) | N |