Ask Question
13 January, 07:12

Define a function romanDigitToInt that converts a single Roman digit to a decimal integer. This function takes an argument as a element of the RomanDigit union type and transforms it into the decimal integer using patter matching. This function should have signature: RomanDigit - > int.

+4
Answers (1)
  1. 13 January, 07:59
    0
    let convertRomanToInt romanNumber : int =

    match romanNumber with

    | 'I' - > 1

    | 'V' - > 5

    | 'X' - > 10

    | 'L' - > 50

    | 'C' - > 100

    | 'D' - > 500

    | 'M' - > 100

    let rec romanNumberToInteger romanNumber =

    if (String. length romanNumber = 1) then

    convertRomanToInt romanNumber.[0]

    elif (String. length romanNumber = 0) then 0

    else let atuple = (romanNumber.[0], romanNumber.[1])

    let f atuple = match atuple with

    | (f, s) when (convertRomanToInt f) > = (convertRomanToInt s) - > (convertRomanToInt f) + (romanNumberToInteger romanNumber.[1 ... ])

    | (f, s) when (convertRomanToInt f) (convertRomanToInt s) - (convertRomanToInt f) + (romanNumberToInteger romanNumber.[2 ... ])

    f atuple

    let charToRomanDigit c =

    printfn "%c" c

    match c with

    | 'I' - > 'I'

    | 'V' - > 'V'

    | 'X' - > 'X'

    | 'L' - > 'L'

    | 'C' - > 'C'

    | 'D' - > 'D'

    | 'M' - > 'M'

    | _ - > failwith "Invalid Entry"

    let stringToNumber str =

    let upperDigit (x:string) = x. ToUpper ()

    let upperString = upperDigit str

    printfn "%s" upperString

    String. collect (fun c - > sprintf "%c" (charToRomanDigit c)) upperString

    let romanNumber = "XIX"

    let upperDigit (x:string) = x. ToUpper ()

    printfn "a: %s" (upperDigit romanNumber)

    romanNumber = stringToNumber romanNumber

    printfn "/nValue = %d" (romanNumberToInteger romanNumber)

    Explanation:

    Create a function called convertRomanToInt for pattern matching to convert romanNumber to valid integer Create a recursive function called romanNumberToInteger and check if length of romanNumber is 1 or 0. Check whether the first character is less than second character, then its reverse otherwise it's normal. Create tuple of first and second character for pattern matching. Create a function charToRomanDigit and use String. collect to go through each character present in string and reconstruct them again. System. Char. ToUpper is used to convert a character to uppercase and stringToNumber to convert string in valid Roman number. Finally print the results.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Define a function romanDigitToInt that converts a single Roman digit to a decimal integer. This function takes an argument as a element of ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers