Created
December 13, 2010 15:06
-
-
Save stevemohapibanks/739068 to your computer and use it in GitHub Desktop.
Outputting roman numerals in Clojure
This file contains hidden or 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
(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