Skip to content

Instantly share code, notes, and snippets.

@sixthgear
Created January 15, 2015 21:56
Show Gist options
  • Select an option

  • Save sixthgear/c4c490e1d74f23146fcf to your computer and use it in GitHub Desktop.

Select an option

Save sixthgear/c4c490e1d74f23146fcf to your computer and use it in GitHub Desktop.

Revisions

  1. sixthgear created this gist Jan 15, 2015.
    18 changes: 18 additions & 0 deletions roman.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    main = getLine >>= \d -> putStrLn (decToRoman (read d :: Integer)) >> main

    decToRoman :: Integer -> String
    decToRoman dec
    | dec >= 1000 = "M" ++ decToRoman(dec - 1000)
    | dec >= 900 = "CM" ++ decToRoman(dec - 900)
    | dec >= 500 = "D" ++ decToRoman(dec - 500)
    | dec >= 400 = "CD" ++ decToRoman(dec - 400)
    | dec >= 100 = "C" ++ decToRoman(dec - 100)
    | dec >= 90 = "XC" ++ decToRoman(dec - 90)
    | dec >= 50 = "L" ++ decToRoman(dec - 50)
    | dec >= 40 = "XL" ++ decToRoman(dec - 40)
    | dec >= 10 = "X" ++ decToRoman(dec - 10)
    | dec >= 9 = "IX" ++ decToRoman(dec - 9)
    | dec >= 5 = "V" ++ decToRoman(dec - 5)
    | dec >= 4 = "IV" ++ decToRoman(dec - 4)
    | dec >= 1 = "I" ++ decToRoman(dec - 1)
    | otherwise = ""