Created
March 16, 2019 20:54
-
-
Save bereal/2bef482568c944ff4a65ea0545544e63 to your computer and use it in GitHub Desktop.
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
import System.Environment | |
letters = "MDCLXVI" | |
values = [1000, 500, 100, 50, 10, 5, 1] | |
-- Convert an int to Roman representation | |
toRoman n = concat $ norm (reverse letters) (coeffs values n []) [] | |
where coeffs (v : vs) n buf = let (val, n') = divMod n v in coeffs vs n' (val:buf) | |
coeffs [] n buf = buf | |
norm (s1:(s2:ss@(s3:_))) (4:(1:(vs))) buf = norm ss vs ([s1, s3]:buf) | |
norm (s1:(s2:ss)) (4:(0:vs)) buf = norm ss vs ([s1, s2]:buf) | |
norm (s1:ss) (v1:vs) buf = norm ss vs ((replicate v1 s1):buf) | |
norm [] _ buf = buf | |
main = getArgs >>= putStrLn . toRoman . read . head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment