Last active
April 5, 2019 04:47
-
-
Save kumo/0cdd0bbbbd428b15c0b98e71bb15f4a5 to your computer and use it in GitHub Desktop.
Second version of arabic to roman numerals converter
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
arabic :: String -> Int | |
arabic "" = 0 | |
arabic (x:y:xs) | |
| x == 'C', y == 'M' = 900 + arabic xs | |
| x == 'C', y == 'D' = 400 + arabic xs | |
| x == 'I', y == 'X' = 9 + arabic xs | |
| x == 'I', y == 'V' = 4 + arabic xs | |
arabic (x:xs) | |
| x == 'M' = 1000 + arabic xs | |
| x == 'D' = 500 + arabic xs | |
| x == 'C' = 100 + arabic xs | |
| x == 'X' = 10 + arabic xs | |
| x == 'V' = 5 + arabic xs | |
| x == 'I' = 1 + arabic xs | |
| otherwise = 0 + arabic xs |
Second version works for numbers like MCMXXV (1925), but unfortunately, also for VIVI, which is invalid, but becomes V (5) + IV (4) + I (1)
Removing repetitions so that code is cleaner than before, but I'm sure the final part could be replaced with a tuple list.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First version doesn't work for combinations like CM, CD, IX, IV.