Hints for writing stingToNumber and numberToString

listToNumber

To help with stringToNumber, write a function listToNumber so that listToNumber b x takes a base b and a list of integer digits x, and produces the number that x represents as a base b number. For example, listToNumber 10 [9,4,2] = 942, listToNumber 10 [5,3,7,6] = 5376 and listToNumber 2 [1,1,0,0] = 12 (since 1100 is 12 in binary). Test listToNumber. [Hint on listToNumber]

stringToNumberHelp

A string is a list of characters. Each character has a code, which is a natural number. Standard function rank will give you the code of a character. For example, rank('a') = 97. If you map rank onto a string, you get a list of numbers. For example, map rank "abc" = [97, 98, 99].

Write a function stringToNumberHelp so that (stringToNumberHelp ranker n (s)) is a number that string s represents, where ranker is a function that converts a character to a string, and n is the largest value that ranker will produce, on the characters that you will allow in strings. All you need to do is to use ranker to convert each character to an integer, and then use listToNum with base n+1. If you define decimalRank(c) = rank(c) -- rank('0'), so that decimalRank('3') = 3, then you should find that (stringToNumberHelp decimalRank 9 ("352") = 352. It uses decimalRank to convert "352" to [3,5,2], then uses listToNumber with base 10 to convert to 352.

Be sure to test stringToNumberHelp. Include examples.

stringToNumber

Notice that listToNumber 10 [0,1] = 1 and listToNum 10 [1] = 1. Leading zeros do not affect the result. But that means that it is impossible to recover the original list from the number. If all you have is the number 1, how do you know whether it came from list [1] or from list [0,1] or [0,0,1], etc.?

An easy way to avoid the problem is to ensure that there are no zeroes in the list. You can do that by choosing a ranker function that only produces positive results.

The characters in our strings are not necessarily decimal digits. They might conceivably be any value that can be put into a byte. (We might even be asked to encipher a binary file.) If you use a ranker function ranker(c) = rank(c) + 1, then ranker will never produce a negative result. The largest that ranker will produce is 256, since the largest number that you can store in one byte is 255.

Use stringToNumberHelp with this ranker function to define stringToNumber.

numberToList and numberToString

You will also need to convert back from a number to a string. Write a function numberToString so that numberToString(stringToNumber(s)) = s. That is, numberToString does the inverse transformation of stringToNumber. The main part of this is a function numberToList so that (numberToList b n) produces the list of digits represented by number n, in base b, without any leading zeros. For example, numberToList 10 942 = [9,4,2]. [Hint for numberToList]