Skip to content

Instantly share code, notes, and snippets.

@kumo
Last active April 5, 2019 04:47
Show Gist options
  • Save kumo/0cdd0bbbbd428b15c0b98e71bb15f4a5 to your computer and use it in GitHub Desktop.
Save kumo/0cdd0bbbbd428b15c0b98e71bb15f4a5 to your computer and use it in GitHub Desktop.
Second version of arabic to roman numerals converter
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
@kumo
Copy link
Author

kumo commented Apr 4, 2019

First version doesn't work for combinations like CM, CD, IX, IV.

@kumo
Copy link
Author

kumo commented Apr 4, 2019

Second version works for numbers like MCMXXV (1925), but unfortunately, also for VIVI, which is invalid, but becomes V (5) + IV (4) + I (1)

@kumo
Copy link
Author

kumo commented Apr 5, 2019

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