Question: In class, we used denotational semantics to evaluate a decimal number using a function Mdec.We could define a similar function to evaluate hexadecimal numbers as
In class, we used denotational semantics to evaluate a decimal number using a function Mdec.We could define a similar function to evaluate hexadecimal numbers as follows:
MhexDigit (0) = 0,..., MhexDigit (9) = 9,
MhexDigit (A) = MhexDigit (a) = 10,...,MhexDigit (B) = MhexDigit (b)
MhexDigit (F) = MhexDigit (f) = 15
Mhex (d) = MhexDigit (d) (Base case one -digit string)
Mhex(dndn-1...d1d0) = Mhex(dndn-1...d1)*16 + MhexDigit(d0)
Note that I am allowing both upper-and lower-case letters for the hexadecimal digits for 10,11,...,15.
Implement a Haskell function mHexDigit to return the numerical value of a single hexadecimal digit. You will need to use guards and the ord function from the Haskell module Data.Char. The Haskell type should be mHexDigit::Char-> Int
Now, implement a Haskell function mHex::String-> Int to evaluate a hexadecimal number given as a string.
Finally, since mHexDigit is only used by mHex, implement a Haskell function Hex::String->Int in which mHexDigit is defined using the where or let construct.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
