DEV Community

Discussion on: Daily Challenge #209 - Roman Numerals

Collapse
 
andreasjakof profile image
Andreas Jakof

if you are going from back to front, it is a bit easier to "parse".
So this time F# instead of my usual C# posts.

Yes, still learning...

let roman list : int = 
    let getNumeralValue x =
        match x with
        | 'M' -> 1000
        | 'm' -> 1000
        | 'D' -> 500
        | 'd' -> 500
        | 'C' -> 100
        | 'c' -> 100
        | 'L' -> 50
        | 'l' -> 50
        | 'X' -> 10
        | 'x' -> 10
        | 'V' -> 5
        | 'v' -> 5
        | 'I' -> 1
        | 'i' -> 1
        | _ -> 0
    let folder (sum,last) current = 
        if last > current 
        then (sum-current,last) 
        else (sum+current,current)
    Seq.rev list 
    |> Seq.map getNumeralValue 
    |> Seq.fold folder (0,0) 
    |> fst