In algebra we learn to formulate dependencies between quantities using
VARIABLE EXPRESSIONS.
A variable is a placeholder that stands for
an unknown quantity. For example, a disk of radius r has the approximate
area
In this expression, r stands for any positive number. If we now come across a disk with radius 5, we can determine its area by substituting 5 for r in the above formula and reducing the resulting expression to a number:
More generally, expressions that contain variables are rules that describe how to compute a number when we are given values for the variables.
A program is such a rule. It is a rule that tells us and the computer how to produce data from some other data. Large programs consist of many small programs and combine them in some manner. It is therefore important that programmers name each rule as they write it down. A good name for our sample expression is area-of-disk. Using this name, we would express the rule for computing the area of a disk as follows:
(define (area-of-disk r) (* 3.14 (* r r)))The two lines say that area-of-disk is a rule, that it consumes a single INPUT, called r, and that the result, or OUTPUT, is going to be (* 3.14 (* r r)) once we know what number r represents.
Programs combine basic operations. In our example, area-of-disk uses only one basic operation, multiplication, but defined programs may use as many operations as necessary. Once we have defined a program, we may use it as if it were a primitive operation. For each variable listed to the right of the program name, we must supply one input. That is, we may write expressions whose operation is area-of-disk followed by a number:
(area-of-disk 5)We also say that we APPLY area-of-disk to 5.
The application of a defined operation is evaluated by copying the expression named area-of-disk and by replacing the variable (r) with the number we supplied (5):
(area-of-disk 5) = (* 3.14 (* 5 5)) = (* 3.14 25) = 78.5
Many programs consume more than one input. Say we wish to define a program that computes the area of a ring, that is, a disk with a hole in the center:
The area of the ring is that of the outer disk minus the area of the inner disk, which means that the program requires two unknown quantities: the outer and the inner radii. Let us call these unknown numbers outer and inner. Then the program that computes the area of a ring is defined as follows:
(define (area-of-ring outer inner)
(- (area-of-disk outer)
(area-of-disk inner)))
The three lines express that area-of-ring is a program, that the
program accepts two inputs, called outer and
inner, and that the result is going to be the difference
between (area-of-disk outer) and (area-of-disk inner). In other words, we have used both basic Scheme operations
and defined programs in the definition of area-of-ring.
When we wish to use area-of-ring, we must supply two inputs:
(area-of-ring 5 3)The expression is evaluated in the same manner as (area-of-disk 5). We copy the expression from the definition of the program and replace the variable with the numbers we supplied:
(area-of-ring 5 3)The rest is plain arithmetic.= (- (area-of-disk 5) (area-of-disk 3))
= (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3))) = ...
Exercise 2.2.1
Define the program Fahrenheit->Celsius,
which consumes a temperature measured
in Fahrenheit and produces the Celsius equivalent. Use a chemistry or
physics book to look up the conversion formula.
When the function is fully developed, test it using the teachpack
convert.ss. The teachpack provides three functions:
convert-gui, convert-repl, and convert-file.
The first creates a graphical user interface. Use it with
(convert-gui Fahrenheit->Celsius)The expression will create a new window in which users can manipulate a slider and buttons.
The second emulates the Interactions window. Users are asked to enter a Fahrenheit temperature, which the program reads, evaluates, and prints. Use it via
(convert-repl Fahrenheit->Celsius)The last operation processes entire files. To use it, create a file with those numbers that are to be converted. Separate the numbers with blank spaces or new lines. The function reads the entire file, converts the numbers, and writes the results into a new file. Here is the expression:
(convert-file ``in.dat'' Fahrenheit->Celsius ``out.dat'')This assumes that the name of the newly created file is in.dat and that we wish the results to be written to the file out.dat. For more information, use DrScheme's Help Desk to look up the teachpack convert.ss. Solution
Exercise 2.2.2
Define the program dollar->euro, which consumes a number of dollars and produces the euro equivalent. Use the currency table in the newspaper to look up the current exchange rate. Solution
Exercise 2.2.3
Define the program triangle. It consumes the length of a triangle's side and its height. The program produces the area of the triangle. Use a geometry book to look up the formula for computing the area of a triangle. Solution
Exercise 2.2.4
Define the program convert3. It consumes three digits, starting with the least significant digit, followed by the next most significant one, and so on. The program produces the corresponding number. For example, the expected value of
(convert3 1 2 3)is 321. Use an algebra book to find out how such a conversion works. Solution
Exercise 2.2.5
A typical exercise in an algebra book asks the reader to evaluate an expression like
for n = 2, n = 5, and n = 9. Using Scheme, we can formulate such an expression as a program and use the program as many times as necessary. Here is the program that corresponds to the above expression:
(define (f n) (+ (/ n 3) 2))First determine the result of the expression at n = 2, n = 5, and n = 9 by hand, then with DrScheme's stepper.
Also formulate the following three expressions as programs: