3 people like it.

Read roman numerals

Function that parses a Roman-numeral string and return the number it represents.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
(*
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";;
val numbers : Map<char,int>

Full name: Script.numbers
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofList : elements:('Key * 'T) list -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofList
val readromannumerals : s:string -> int

Full name: Script.readromannumerals
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
module Array

from Microsoft.FSharp.Collections
val foldBack : folder:('T -> 'State -> 'State) -> array:'T [] -> state:'State -> 'State

Full name: Microsoft.FSharp.Collections.Array.foldBack
val ele : char
val prevalue : int
val total : int
val currentvalue : int
property Map.Item: char -> int
System.String.ToCharArray() : char []
System.String.ToCharArray(startIndex: int, length: int) : char []
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
Raw view Test code New version

More information

Link:http://fssnip.net/8b
Posted:12 years ago
Author:Naveen
Tags: strings