Skip to content

Instantly share code, notes, and snippets.

@ichiban
Created April 28, 2011 05:07
Show Gist options
  • Save ichiban/945835 to your computer and use it in GitHub Desktop.
Save ichiban/945835 to your computer and use it in GitHub Desktop.
(require :fare-matcher)
;; Clojure or ML's function syntax style defun
(defmacro defunction (name &body body)
(flet ((match-clause (clause)
(cons `(list ,@(car clause)) (cdr clause))))
(let ((args (gensym)))
`(defun ,name (&rest ,args)
(fare-matcher:ematch ,args
,@(mapcar #'match-clause body))))))
;; examples
(defunction f
((:ping) :pong)
((:echo x) x))
(f :ping) ; ==> :pong
(f :echo "hi") ; ==> "hi"
(f 1 2 3) ; ==> raise error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment