Integer Conversion in Haskell. HELP!! The method that I have to implement is con
ID: 3764228 • Letter: I
Question
Integer Conversion in Haskell. HELP!!
The method that I have to implement is
convert numString b1 b2
such that it returns a string representing the value in numString (represented in base b1). It should return a string with the number represented in base b2.
You may not, of course, use any library functions that do all the work for you.
You may use digitToInt (but it only works on '0'-'9' and 'A'-'F'). Make sure you write "import Data.Char" at the top of your file
We want to make our function work with bases up to 35. The way we will do this is to let A=10, B = 11, ... Z= 35.
['A','B'..'Z'] is the list of characters from A to Z (you can do the same for the digit characters).
elem will test if the first argument is a member of the second, list, argument
Here are the functions I defined to solve this (you only have to have convert fully functional):
convert numString b1 b2 -- of course
valNumString (xs) b1 --to get the value of a string
int2Base n b -- integer to string at base b
num2char n -- get character for numbers in range... a mapping from [0..35] to [ '0'..'9','A'..'Z']
dig2Int dChar -- converts character to integer
indexInto x (y:ys) --gets the index of a value into a list, e.g. indexInto 4 [0..4] = 4
Test Cases:
*Main> convert "123" 10 10
"123"
*Main> convert "123" 8 10
"83"
*Main> convert "F" 16 10
"15"
*Main> convert "FF" 16 10
"255"
*Main> convert "XXX" 35 10
"41613"*
Main> 33*35*35+33*35+33
41613
Explanation / Answer
Prelude> (reads "123 abc") :: [(Integer, String)]
[(123," abc")]
Prelude> show 123
"123"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.