Last active
August 29, 2015 14:23
-
-
Save CRogers/ff13285fe67acec9ad6a to your computer and use it in GitHub Desktop.
Roman Numerals in Haskell
This file contains 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
module Main where | |
import Data.List | |
data Numeral = Numeral Int String | |
numerals :: [Numeral] | |
numerals = reverse [ | |
Numeral 1 "I", | |
Numeral 4 "IV", | |
Numeral 5 "V", | |
Numeral 9 "IX", | |
Numeral 10 "X" | |
] | |
numeral :: Int -> Maybe (String, Int) | |
numeral x = fmap next $ find predicate numerals | |
where | |
predicate (Numeral value _) = x >= value | |
next (Numeral value numeral) = (numeral, x - value) | |
romanNumeral :: Int -> String | |
romanNumeral x = concat $ unfoldr numeral x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment