common-lisp Pattern matching Constructor patterns


Example

Cons-cells, structures, vectors, lists and such can be matched with constructor patterns.

(loop for i from 1 to 30
      do (format t "~5<~a~;~>"
                 (match (cons (mod i 3)
                              (mod i 5))
                   ((cons 0 0) "Fizzbuzz")
                   ((cons 0 _) "Fizz")
                   ((cons _ 0) "Buzz")
                   (_ i)))
      when (zerop (mod i 5)) do (terpri))
; 1    2    Fizz 4    Buzz 
; Fizz 7    8    Fizz Buzz 
; 11   Fizz 13   14   Fizzbuzz
; 16   17   Fizz 19   Buzz 
; Fizz 22   23   Fizz Buzz 
; 26   Fizz 28   29   Fizzbuzz