Created
February 23, 2011 13:25
-
-
Save apg/840427 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro match | |
"shitty pattern matching macro | |
Example: | |
(def eval [exp] | |
(match exp | |
(:var v) v | |
(:abstraction arg body) (create-procedure arg body) | |
(:application operator operand) (operator operand))) | |
" | |
[exp & body] | |
(let [pairs (partition 2 body) | |
tests (map (fn [[t b]] (first t)) pairs) | |
bodies (map (fn [[t b]] | |
`(let [[~(gensym) ~@(rest t)] ~exp] | |
~b)) | |
pairs)] | |
`(case (first ~exp) | |
~@(mapcat #(vector %1 %2) tests bodies)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment