Last active
August 29, 2015 14:22
-
-
Save d11wtq/507c99aa57d2c90ff875 to your computer and use it in GitHub Desktop.
Roman numerals
This file contains 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
(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