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:
-135/7(38 plus 29/3)(0 divided-by (162 minus 13))(((-71/2 times 63/8) plus 7/43) divided-by (85/134 minus
-67/23))
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:
The value of any exact rational number is that number itself.
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.
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.
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.
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.
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/~stone/courses/scheme/exercise-4.html
created September 24, 1997
last revised September 24, 1997