Skip to content

Instantly share code, notes, and snippets.

@guipn
Created October 8, 2012 02:26
Show Gist options
  • Save guipn/3850411 to your computer and use it in GitHub Desktop.
Save guipn/3850411 to your computer and use it in GitHub Desktop.
Roman Notation
trivial :: [(Int, String)]
trivial = [
{- Bridge values -}
(1, "I"), (5, "V"), (10, "X"), (50, "L"),
(100, "C"), (500, "D"), (1000, "M"),
{- Subtractions -}
(4, "IV"), (9, "IX"), (40, "XL"), (90, "XC"),
(400, "CD"), (900, "CM")
]
decimals :: [Int]
decimals = map fst trivial
mapTrivial :: Int -> String
mapTrivial n = snd . head $ filter (\ (dec, _) -> dec == n) trivial
toRoman :: Int -> String
toRoman n | n `elem` decimals = mapTrivial n
| otherwise = mapTrivial nextRoman ++ toRoman (n - nextRoman)
where nextRoman = maximum $ filter (< n) decimals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment