Skip to content

Instantly share code, notes, and snippets.

@LNA
Last active August 29, 2015 14:17
Show Gist options
  • Save LNA/c68e7fcc5833a4cb0f36 to your computer and use it in GitHub Desktop.
Save LNA/c68e7fcc5833a4cb0f36 to your computer and use it in GitHub Desktop.
Roman Numeral Kata in Clojure
; 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