Skip to content

Instantly share code, notes, and snippets.

@joxn
Created June 10, 2016 04:45
Show Gist options
  • Save joxn/fe43770ef222ea2ade9de1111825243a to your computer and use it in GitHub Desktop.
Save joxn/fe43770ef222ea2ade9de1111825243a to your computer and use it in GitHub Desktop.
Given a positive Integer less than 4999, produce the Roman numeral representation.
{-# OPTIONS_GHC -Wall -Werror #-}
module Roman( numerals ) where
import Data.List( unfoldr )
numerals :: Integer -> String
numerals = concat . unfoldr getNextRoman
where
getNextRoman n | n >= 1000 = Just ("M", n - 1000)
| n >= 900 = Just ("CM", n - 900)
| n >= 500 = Just ("D", n - 500)
| n >= 400 = Just ("CD", n - 400)
| n >= 100 = Just ("C", n - 100)
| n >= 90 = Just ("XC", n - 90)
| n >= 50 = Just ("L", n - 50)
| n >= 40 = Just ("XL", n - 40)
| n >= 10 = Just ("X", n - 10)
| n >= 9 = Just ("IX", n - 9)
| n >= 5 = Just ("V", n - 5)
| n >= 4 = Just ("IV", n - 4)
| n >= 1 = Just ("I", n - 1)
| otherwise = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment