Lab Module B -- Writing linear recursive procedures

CISC 280 Program Development Techniques

Goals:
  1. Be able to write linearly recursive procedures. These are procedures which call themselves. The "linear recursive" is as opposed to "tree recursive". Linear recursive procedures call themselves once. When the one call is evaluated, it may in turn call the procedure again, so that the process achieves a repetition much as with for loops or while loops in C++ and Java. On the other hand tree recursive procedures call themselves two or more times and combine the results.
  2. To be able to translate back and forth between C++ functions using loops and linear recursive Scheme procedures.
  3. Be able to determine if a linearly recursive procedure is tail recursive, that is, if it's evaluation will induce an iterative process or not. Definition: an iterative process is one that uses a fixed amount of memory, even though the procedure specifies repetition by being written in a linearly recursive way or by using loop constructs such as C++ while.
  4. Be able to rewrite a linear recursive procedure which does not induce an iterative process as a (linear recursive) procedure(s) which does induce an iterative process.

Reading: SICP (Structure and Interpretation of Computer Programs), Section 1.2.1
Scan DrScheme online help to understand the two part DrScheme window and the "language levels" setup.

Homework exercises (Due at start of lab 19 Feb 01):

  1. Consider these two functions:

    (define (least-digit n) (remainder n 10))
    (define (higher-digits n) (quotient n 10))

    For a positive integer n, (least-digit n) returns the unit's digit of n, and (higher-digits n) returns the other digits of n. Thus for n = 1234567, (least-digit n) is 7 and (higher-digits n) is 123456. Write a Scheme procedure, call it digit-sum, to compute the sum of the digits of a number.

  2. The (digital-root n) is obtained by taking (digit-sum n), then taking the digit sum of that, etc., repeatedly until the result is a single digit. Define this procedure. Print your definitions for these first two problems and also print interactions testing them. Give some interactions in which the input number is quite large, say 20 or more digits. By the way, it is said that the digital root of a number is 9 if and only if the number is divisible by 9. Do your experiments confirm this?

  3. Write digital-root as a C++ function. Don't use recursion, but you may write digit-sum as a separate function or write digital-root as one function having a nested loop structure. This is a pencil and paper exercise. You don't have to test your function online, but do write it with good clear indentation and style.