https://github.com/fitzgen/cl-pattern-matching
Erlang/Haskell style pattern matching macros for Common Lisp.
https://github.com/fitzgen/cl-pattern-matching
Last synced: over 1 year ago
JSON representation
Erlang/Haskell style pattern matching macros for Common Lisp.
- Host: GitHub
- URL: https://github.com/fitzgen/cl-pattern-matching
- Owner: fitzgen
- Created: 2010-05-20T18:53:26.000Z (about 16 years ago)
- Default Branch: master
- Last Pushed: 2012-11-16T18:53:19.000Z (over 13 years ago)
- Last Synced: 2025-03-17T11:59:39.432Z (over 1 year ago)
- Language: Common Lisp
- Homepage:
- Size: 101 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
CL-Pattern-Matching
===================
`let-match`
===========
CL-USER> (let-match ((1 2 ?three) '(1 2 3))
(* ?three ?three))
9
T
CL-USER> (let-match ((1 2 ?three) '(3 2 1))
(* ?three ?three))
NIL
NIL
CL-USER> (let-match ((1 2 ?three) '(1 2 3))
nil)
NIL
T
`cond-match`
============
CL-USER> (defvar form nil)
FORM
CL-USER> (cond-match form
(nil "Nil!")
(1 "One!")
(?x ?x))
"Nil!"
CL-USER> (setf form 1)
1
CL-USER> (cond-match form
(nil "Nil!")
(1 "One!")
(?x ?x))
"One!"
CL-USER> (setf form "Yo mama!")
"Yo mama!"
CL-USER> (cond-match form
(nil "Nil!")
(1 "One!")
(?x ?x))
"Yo mama!"
`defun-match`
=============
CL-USER> (defun-match desc-args
(nil "No args!")
((?x) "One argument!")
((?x ?y) "Two arguments!")
((?x ?y . ?z) "More than two arguments!"))
DESC-ARGS
CL-USER> (desc-args)
"No args!"
CL-USER> (desc-args 1)
"One argument!"
CL-USER> (desc-args 1 2)
"Two arguments!"
CL-USER> (desc-args 1 2 3)
"More than two arguments!"
CL-USER> (defun-match my-map
((?fn nil) nil)
((?fn (?car . ?cdr)) (cons (funcall ?fn ?car)
(my-map ?fn ?cdr))))
MY-MAP
CL-USER> (my-map #'(lambda (x) (* x x))
'(1 2 3 4 5 6))
(1 4 9 16 25 36)
CL-USER> (defun-match factorial
((0) 1)
((?x) (* ?x (factorial (- ?x 1)))))
FACTORIAL
CL-USER> (factorial 1)
1
CL-USER> (factorial 3)
6
CL-USER> (factorial 5)
120
CL-USER> (factorial 7)
5040