Skip to content

Latest commit

 

History

History
47 lines (41 loc) · 1.73 KB

File metadata and controls

47 lines (41 loc) · 1.73 KB

Pattern-Match

Simple pattern-matching for Common Lisp

Usage

(pattern-case KEYFORM (PATTERN FORM*)*)

Examples

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

Patterns

  • Literal values are typechecked; symbols aren’t.
  • By convention _ is an ignored term and remains unbound; multiple _ may appear in PATTERN.
PatternMatches
tgeneralized boolean (i.e. non-null)
nilitself
:keyworditself
_anything
other symbolanything; bind to expression
other atomitself

Examples

PatternExpressionMatch?Binds
00Y
00.0N
(n . ns)(1 2 3)Yn => 1 ns => (2 3)
(n . ns)(1)Yn => 1 ns => ()
(a b)(BATMAN ROBIN)Ya => BATMAN b => ROBIN
(a b)(LARRY MOE CURLY)N