Skip to content

Instantly share code, notes, and snippets.

@naveensrinivasan
Created September 22, 2011 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveensrinivasan/1235671 to your computer and use it in GitHub Desktop.
Save naveensrinivasan/1235671 to your computer and use it in GitHub Desktop.
Read roman numerals in F#
(*
http://4clojure.com/problem/92
Roman numerals are easy to recognize, but not everyone knows all the rules necessary to work with them.
Write a function to parse a Roman-numeral string and return the number it represents.
You can assume that the input will be well-formed, in upper-case, and follow the subtractive principle.
You don't need to handle any numbers greater than MMMCMXCIX (3999), the largest number representable with ordinary letters.
(= 14 (__ "XIV"))
(= 827 (__ "DCCCXXVII"))
(= 3999 (__ "MMMCMXCIX"))
(= 48 (__ "XLVIII"))
*)
let numbers = Map.ofList [('I',1);('V',5);('X',10);('L',50);('C',100);('D',500);('M',1000)]
let readromannumerals (s:string) =
Array.foldBack(fun ele (prevalue,total) ->
let currentvalue = numbers.Item ele
if currentvalue >= prevalue then (currentvalue, total+currentvalue) else (currentvalue ,total - currentvalue))
(s.ToCharArray()) (0,0)
|> snd
readromannumerals "XIV";;
readromannumerals "DCCCXXVII";;
readromannumerals "MMMCMXCIX";;
readromannumerals "XLVIII";;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment