Created
June 10, 2016 04:45
-
-
Save joxn/fe43770ef222ea2ade9de1111825243a to your computer and use it in GitHub Desktop.
Given a positive Integer less than 4999, produce the Roman numeral representation.
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
{-# 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