EXERCISE SET #2

1. Define a Scheme procedure attach-at-end that takes any value as its first argument and a list as its second argument and returns a list containing all of the the same elements as the given list except that the given value has been added as the last element.

2. Define a Scheme procedure concatenate that takes any two lists as arguments and returns a single list containing all of the elements of the first list, in order, followed by all of the elements of the second list, in order. (Scheme already has a built-in procedure called append that does this. Don't use it.)

3. Define a Scheme procedure position that takes any value as its first argument and a list as its second argument and returns the number of elements of the given list that precede the first occurrence of the given value, or #f if the given value does not occur at all on the given list.

4. Define a Scheme procedure mean that takes a non-empty list of numbers and returns the arithmetic mean of its elements.

5. Adapt the mean procedure so that it calls the error procedure if it is given an empty list.

6. Adapt the mean procedure so that it takes any list as an argument and returns the arithmetic mean of just the elements that are numbers, ignoring the rest. It should call error if the list contains no numbers.

7. Define a Scheme procedure iota that takes a natural number and produces a list, in ascending order, of all the smaller natural numbers. For instance, the value of (iota 5) should be (0 1 2 3 4); the value of (iota 1) should be (0); and the value of (iota 0) should be the empty list.

8. Using the procedures presented in chapter 3 of the textbook for rational arithmetic (make-ratl, numr, and denr), define a Scheme procedure rpower that takes a rational number as its first argument and an integer (which may be positive, zero, or negative) as its second argument and returns a rational number representing the result of raising the given rational number to the power of the given integer.

9. Similarly, using those procedures, define a Scheme procedure rfloor that takes any rational number as its first argument and returns the greatest integer not greater than that rational number. (I'd prefer an exact integer, so use floor only as a last resort -- there is a way to do it without floor.)

10. As described in exercise 3.17 in the textbook (page 93), write versions of the make-ratl, numr, and denr procedures that will ensure that the internal representation of each rational number is unique by forcing the denominator to be positive and reducing the fraction to lowest terms.


created June 10, 1996
last revised June 11, 1996

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