Exercise #4

In this exercise, I'll use the term arithmetic formula to refer to a Scheme datum that is either an exact rational number or a three-element list in which the first and third elements are arithmetic formulas and the second element is one of the symbols plus, minus, times, and divided-by. So, for instance, each of the following is an arithmetic formula:

The exercise is to design, write, and test two Scheme procedures. The first, arithmetic-formula?, is a predicate that takes any Scheme object as its argument and determines whether it is an arithmetic formula, returning #t if it is and #f if it is not. The second, value-of-arithmetic-formula, takes any arithmetic formula as its argument and returns its value, as determined by the following rules:

  1. The value of any exact rational number is that number itself.

  2. The value of any arithmetic formula in which the second element is the symbol plus is the sum of the values of the first and third elements.

  3. The value of any arithmetic formula in which the second element is the symbol minus is the result of subtracting the value of the third element from the value of the first element.

  4. The value of any arithmetic formula in which the second element is the symbol times is the product of the values of the first and third elements.

  5. Any arithmetic formula in which the second element is the symbol divided-by and the value of the third element is 0 has 0 as its value.

  6. The value of any arithmetic formula in which the second element is the symbol divided-by and the value of the third element is not 0 is the result of dividing the value of the first element by the value of the third element.

Choose your own test cases, bearing in mind that you must demonstrate to my satisfaction that your procedures deliver correct answers in all cases.


This document is available on the World Wide Web as

http://www.math.grin.edu/courses/Scheme/fall-1997/exercise-4.html

created September 24, 1997
last revised September 24, 1997

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