Skip to content

Instantly share code, notes, and snippets.

@CRogers
Last active August 29, 2015 14:23
Show Gist options
  • Save CRogers/ff13285fe67acec9ad6a to your computer and use it in GitHub Desktop.
Save CRogers/ff13285fe67acec9ad6a to your computer and use it in GitHub Desktop.
Roman Numerals in Haskell
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