Tutorial by Examples

The two main libraries providing pattern matching in Common Lisp are Optima and Trivia. Both provide a similar matching API and syntax. However trivia provides a unified interface to extend matching, defpattern.
Because a clack request is represented as a plist, we can use pattern matching as the entry point to the clack app as a way to route request to their appropriate controllers (defvar *app* (lambda (env) (match env ((plist :request-method :get :request-uri uri) (...
Using pattern matching one can intertwine function definition and pattern matching, similar to SML. (trivia:defun-match fib (index) "Return the corresponding term for INDEX." (0 1) (1 1) (index (+ (fib (1- index)) (fib (- index 2))))) (fib 5) ;; => 8
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")...
Guard patterns can be used to check that a value satisfies an arbitrary test-form. (dotimes (i 5) (format t "~d: ~a~%" i (match i ((guard x (oddp x)) "Odd!") (_ "Even!")))) ; 0: Even! ; 1: Odd! ; 2: Even! ; 3: Odd! ; 4: ...

Page 1 of 1