Skip to content

Instantly share code, notes, and snippets.

@tippenein
Created June 4, 2016 23:19
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 tippenein/f05debf299feb72a8d5e6f4c124fd8cd to your computer and use it in GitHub Desktop.
Save tippenein/f05debf299feb72a8d5e6f4c124fd8cd to your computer and use it in GitHub Desktop.
Not sure where this problem came from, but it was in an old folder
import Control.Monad
romanValues = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
romanDigits = words "m cm d cd c xc l xl x ix v iv i"
theRomans = zip romanValues romanDigits
toRoman' :: Int -> String
toRoman' n = fst $ foldl romFold ("", n) theRomans
romFold :: ([a], Int) -> (Int, [a]) -> ([a], Int)
romFold (acc, n) (romValue, romDigit) =
let (result, remain) = divMod n romValue
newDigits = concat $ replicate result romDigit
in (acc ++ newDigits, remain)
romanRangeCheck :: Int -> Bool
romanRangeCheck n
| 0 < n && n < 4000 = True
| otherwise = error "out of bounds [0 < n < 4000]"
toRoman :: Int -> String
toRoman n = do
guard (romanRangeCheck n)
toRoman' n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment