Last active
August 29, 2015 14:17
-
-
Save LNA/c68e7fcc5833a4cb0f36 to your computer and use it in GitHub Desktop.
Roman Numeral Kata in Clojure
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
; The big change in logic happens at 5. You can use nested if statements to solve the kata up to 5. | |
; After that, its best to define the roman numerals and their numbers in pairs, and pull from that. | |
(def pairs [[10 "X"][9 "IX"][5 "V"] [4 "IV"] [1 "I"]]) | |
(defn roman [n] | |
(loop [n n | |
result "" | |
pairs pairs] | |
(if (< n 1) | |
result | |
(let [[num-in-pair roman-numeral] (first pairs)] | |
(if (> num-in-pair n) | |
(recur n result (rest pairs)) | |
(recur (- n num-in-pair) (str result roman-numeral) pairs)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment