Skip to content

Instantly share code, notes, and snippets.

@stevemohapibanks
Created December 13, 2010 15:06
Show Gist options
  • Save stevemohapibanks/739068 to your computer and use it in GitHub Desktop.
Save stevemohapibanks/739068 to your computer and use it in GitHub Desktop.
Outputting roman numerals in Clojure
(def *all-roman* ["M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I"])
(def *all-decimal* [1000 900 500 400 100 90 50 40 10 9 5 4 1])
(defn output-as-roman
([n] (output-as-roman n *all-roman* *all-decimal*))
([n rx nx]
(let [roman (first rx) divisor (first nx)
remainder (mod n divisor) quotient (quot n divisor)]
(str (apply str (repeat quotient roman))
(if (> remainder 0) (output-as-roman remainder (rest rx) (rest nx)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment