Skip to content

Instantly share code, notes, and snippets.

@apg
Created February 23, 2011 13:25
Show Gist options
  • Save apg/840427 to your computer and use it in GitHub Desktop.
Save apg/840427 to your computer and use it in GitHub Desktop.
(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