Skip to content

andreareina/pattern-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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

About

Simple pattern matching in Common Lisp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published