CISC-280 Program Development Techniques

Homework 2: Linear Recursion, Iteration, and Orders of Growth

Due: Thursday, September 25, 2008

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 n
(define (units-digit n)
  (remainder n 10))

;; Returns all but the units digit of the integer n
(define (all-but-units-digit n)
  (quotient n 10))

With 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:

  1. (decimal-length n) returns k+1, where dk is the leading digit of n. For example,
    (decimal-length 348567) --> 6
  2. (ith-digit n i) returns di of n. For example
    (ith-digit 671835629 3) --> 5
  3. (leading-digit n) returns dk, the most significant digit.
    (leading-digit 534) --> 5
  4. (occurances d n) returns the number of times digit d occurs in n.
    (occurrances 6 5494576548754987587654655478563) --> 4
  5. (digit-sum n) returns the sum of the digits in n.
    (digit-sum 2354) --> 14, (digit-sum 8) --> 8
  6. (digital-root n) returns the result of repeatedly applying digit-sum until the result is a single digit.
    (digital-root 341943) --> 6 (via 3+4+1+9+4+3=24, then 2+4=6)
  7. (backwards n) returns the digits in reverse order. You do not need to handle leading or trailing zeros. For this problem you may use multiplication. For example:
    (backwards 329145) --> 541923

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!