Skip to content

Instantly share code, notes, and snippets.

@cesare
Created May 3, 2012 16:11
Show Gist options
  • Save cesare/2586858 to your computer and use it in GitHub Desktop.
Save cesare/2586858 to your computer and use it in GitHub Desktop.
import Data.List
intToRoman :: Int -> String
intToRoman = concat . reverse . (zipWith (\f d -> f d) fs) . split
where
split = unfoldr (\n -> if n > 0 then Just ((mod n 10), (div n 10)) else Nothing)
fs = map roman [('i', 'v', 'x'), ('x', 'l', 'c'), ('c', 'd', 'm'), ('m', '$', '#')]
roman (one, five, ten) n
| n >= 0 && n <= 3 = replicate n one
| n == 4 = [one, five]
| n >= 5 && n <= 8 = five:(replicate (n - 5) one)
| n == 9 = [one, ten]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment