Skip to content

Instantly share code, notes, and snippets.

@remydagostino
Last active August 12, 2016 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remydagostino/d4325341f7539159462235e86be6fda2 to your computer and use it in GitHub Desktop.
Save remydagostino/d4325341f7539159462235e86be6fda2 to your computer and use it in GitHub Desktop.
Calculate the roman numeral string for a number
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))
@remydagostino
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment