Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Last active October 27, 2015 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/5ad6b67bd07dce0f6cc7 to your computer and use it in GitHub Desktop.
Save lepoetemaudit/5ad6b67bd07dce0f6cc7 to your computer and use it in GitHub Desktop.
Macro for polish notation (prefix, i.e. not postfix RPN) in Clojure
(defmacro pln [& args]
(let [operator-count (dec (/ (count args) 2))
operators (take operator-count args)
values (drop operator-count args)
op-vals (map vector operators (next values))]
(loop [form (first values)
op-vals op-vals]
(if (not-empty op-vals)
(let [[op val] (first op-vals)]
(recur
(list op form val)
(rest op-vals)))
form))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment