CISC-280 Program Development Techniques
Practice with linear recursion and iteration.
Since the only data structure we know now are numbers, we can practice recursion and iteration by operating on the digits that make up numbers. Later in the course, we'll use very similar ideas on a general data structure called a list.
Any positive number n can be written as a sequence of digits dk dk-1 ...d1 d0, where dk is non-zero. Thus the number n is the sum of di times 10 to the i power, for i = 0 to k. For example, if n = 456, then k is 2 and d2 is 4, d1 is 5, and d0 is 6. A Scheme program can access the individual digits by using the following basic functions:
;; Returns the units digit of the integer nWith those definitions in place, for example,
(unit-digit 456) --> 6
(all-but-units-digit 456) --> 45
By combining these functions, you can access the rightmost (units) digit, the second digit, the third digit, etc. ultimately reaching the most significant (leftmost) digit. If (all-but-the-units-digit n) is zero, you know n is a one-digit number.
Using these access functions, define the following functions:
Hand in a printout of your definitions along with the check-expects. Do not forget to provide appropriate documentation of each procedure. In addition, indicate whether each function implements a linear recursive procedure or an iterative procedure. Hint: Each of these functions should be fairly short, simple functions. If they get long, re-examine the problem!