Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Last active August 29, 2015 14:22
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 d11wtq/507c99aa57d2c90ff875 to your computer and use it in GitHub Desktop.
Save d11wtq/507c99aa57d2c90ff875 to your computer and use it in GitHub Desktop.
Roman numerals
(ns roman.core)
(defmacro defpairs
"Helper macro to make ordered defs for pairs nicer."
[varname pairs]
`(def ~varname
(partition 2 ~pairs)))
(defpairs digits
["M" 1000
"D" 500
"C" 100
"L" 50
"X" 10
"V" 5
"I" 1])
(defn numerals
"Convert integer n to roman numerals."
[n]
(loop [n n, acc ""]
(if (zero? n)
acc
(let [[s v] (some #(if (<= (second %) n) %)
digits)]
(recur (- n v) (str acc s))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment