EXERCISE SET #3

1. Define a Scheme procedure quadratic that takes three arguments a, b, and c, all real numbers, and returns a list of the roots of the quadratic equation ax^2 + bx + c = 0.

2. Define a Scheme procedure subst that takes three arguments (two arbitrary values and a list) and returns a list just like the given list except that every occurrence of the first value has been replaced with the second value. For instance, the value of the call (subst 'c 'k '(c o c o n u t)) should be (k o k o n u t).

3. Define a Scheme procedure list-segment that takes three arguments -- the first a list, the second a natural number no greater than the length of the list, and the third a natural number no less than the second argument but no greater than the length of the list -- and returns a segment of the given list, starting after the number of elements indicated by the second argument and stopping after the element indicated by the third argument. For instance, the value of the call (list-segment '(a b c d e f g) 3 5) should be (d e) -- the segment of (a b c d e f g) that starts after the third element and stops at the fifth. If the second and third elements are equal, list-segment should return the empty list.

4. Define a Scheme procedure range-sum that takes two integer arguments and finds the sum of all the integers greater than or equal to the first argument and less than or equal to the second. For instance, the value of the call (range-sum 12 16) should be 70 (that is, 12 + 13 + 14 + 15 + 16).

5. Let's define a numeric structure, recursively, as a value that is either the null object, or a real number, or a pair of numeric structures. Define a Scheme procedure that finds the sum of all the real numbers that occur anywhere inside a given numeric structure.

6. Define a Scheme procedure that finds that greatest real number in a given numeric structure. (If the structure contains no real numbers -- for instance, if it is the null object -- the procedure should return #f.)

7. Similarly, let's define a symbolic structure as a value that is either the null object, or a symbol, or a pair of symbolic structures. Define a Scheme procedure contents that takes any symbolic structure as its argument and returns a list of all the symbols that occur anywhere inside that symbolic structure.

8. Here's an alternative definition of a symbolic structure: a value that is either a symbol or a list of symbolic structures. Is this definition equivalent to the one proposed in the preceding exercise? Will your definition of contents still work for symbolic structures in the new sense? If not, how would you revise it?

9. Define a Scheme procedure sum that yields the result described in exercise 3.1 on page 81 of the text, but obtains that result by an iterative process (as described in section 4.5).

10. Define the iota procedure (exercise set #2, number 7) so that it obtains its result by an iterative process.


created June 11, 1996
last revised June 11, 1996

John David Stone (stone@math.grin.edu)