Last active
August 12, 2016 22:50
-
-
Save remydagostino/d4325341f7539159462235e86be6fda2 to your computer and use it in GitHub Desktop.
Calculate the roman numeral string for a number
This file contains hidden or 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 RomanNumerals.Simple (integerToRomanString) where | |
import Data.List (find) | |
import Data.Maybe (maybe) | |
integerToRomanString :: Integer -> Maybe String | |
integerToRomanString v = | |
let | |
sortedVals :: [(String, Integer)] | |
sortedVals = | |
[("MMM", 3000) | |
,("MM", 2000) | |
,("M", 1000) | |
,("CM", 900) | |
,("D", 500) | |
,("CD", 400) | |
,("CCC", 300) | |
,("CC", 200) | |
,("C", 100) | |
,("XC", 90) | |
,("L", 50) | |
,("XL", 40) | |
,("XXX", 30) | |
,("XX", 20) | |
,("X", 10) | |
,("IX", 9) | |
,("V", 5) | |
,("IV", 4) | |
,("III", 3) | |
,("II", 2) | |
,("I", 1) | |
] | |
in do | |
(str, num) <- find ((1 ==) . div v . snd) sortedVals | |
return $ maybe str (str ++) (integerToRomanString (v - num)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@starkcoffee 😄