Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Created November 30, 2012 19:10
Show Gist options
  • Save iolloyd/d77fab3080e47eb5d426 to your computer and use it in GitHub Desktop.
Save iolloyd/d77fab3080e47eb5d426 to your computer and use it in GitHub Desktop.
Roman numerals conversion to decimal in Scala
def numerals: List[char]): int = in match {
case 'i' :: 'v' :: xs => 4 + numerals(xs)
case 'i' :: 'X' :: xs => 9 + numerals(xs)
case 'i' :: xs => 1 + numerals(xs)
case 'v' :: xs => 5 + numerals(xs)
case 'X' :: 'L' :: xs => 40 + numerals(xs)
case 'X' :: 'c' :: xs => 90 + numerals(xs)
case 'X' :: xs => 10 + numerals(xs)
case 'L' :: xs => 50 + numerals(xs)
case 'c' :: 'd' :: xs => 400 + numerals(xs)
case 'c' :: 'm' :: xs => 900 + numerals(xs)
case 'c' :: xs => 100 + numerals(xs)
case 'd' :: xs => 500 + numerals(xs)
case 'm' :: xs => 1000 + numerals(xs)
case _ => 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment