What is the most clear and idiomatic way to convert arabic numbers to
roman notation? My take:
(def roman
(hash-map
1 "I",
4 "IV",
5 "V",
9 "IX",
10 "X",
40 "XL",
50 "L",
90 "XC",
100 "C",
400 "CD",
500 "D",
900 "CM",
1000 "M"
)
)
(defn to-roman
([n]
(to-roman n 1000))
([n divisor]
(if (zero? divisor)
""
(let [times (quot n divisor)
large-component (* times divisor)
small-component (- n large-component)]
(str
(if (roman large-component)
(roman large-component)
(apply str (repeat times (roman divisor))))
(to-roman small-component (quot divisor 10)))))))
(to-roman 3991)
=> "MMMCMXCI"
I am aware of tail recursion problem in JVM but in this particular
case it's very shallow so that shouldn't be an issue.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en