CS 3 (Garcia) Spring 2001 Lab assignment 2
This lab is intended to give you practice with defining functions and evaluating expressions. You will use a tool called the "Replacement Modeler", which performs the step-by-step replacement of expressions described on page 33 on Simply Scheme.
Read chapters 3 and 4 in Simply Scheme, along with the instructions on the other side of this assignment for using the Replacement Modeler.
Exercise 1 (1 checkoff point)
With your lab partner, activate the Replacement Modeler on the expression used in the example given in the instructions on the other side of this assignment. Then give commands that result in the expression
(- 15 4.)
being displayed somewhere in the Replacement Modeler window. Show the window to your lab t.a. to earn the checkoff point.
Exercise 2 (1 checkoff point)
Verify the five pitfalls discussed on pages 51 and 52, by doing the following:
- First type the incorrect function definitions into an editor window.
- Then evaluate each definition and, if appropriate, test it (in the Interaction window).
One member of your partnership should do two of the pitfalls and the other should do the three that remain. Compare notes with your partner, then show the results to your lab t.a. to earn the checkoff point.
Exercise 3 (1 checkoff point)
In an editor window, type the definitions given in exercise 4.4 on page 54 of Simply Scheme. Whichever member of your partnership did two of the pitfalls in exercise should do three of these, and vice-versa. Evaluate the definitions and test them if necessary to produce erroneous behavior. Show the results to your lab t.a. He or she will ask you to explain the results, then fix one or two of the definitions in order to earn the checkoff point.
Exercise 4 (1 checkoff point)
One member of your partnership should write a function that, given a Celsius temperature as argument, returns the equivalent Fahrenheit temperature. The other partnership member should write an analogous function that, given a Fahrenheit temperature as argument, returns the corresponding Celsius temperature. (The formulas appear in exercise 4.5 on page 54 of Simply Scheme.)
Then load both definitions into Gambit and verify, using temperatures below freezing, at freezing, and above freezing, that the two functions are inverses, i.e. that if a temperature is given as argument to one of the functions and the result is given as argument to the other, the original temperature is returned. ("Freezing" is 32° Fahrenheit and 0° Celsius.) You should evaluate a single expression for each test, using the two functions in composition. Use the Replacement Modeler for at least one of your tests. Show your six tests to your lab t.a. for checkoff.
Review your solutions with your lab partner, and discuss with him or her anything you don't understand. If there are things that both of you don't understand, clear them up with your lab t.a. before you leave.
Instructions for using the Replacement Modeler
The Replacement Modeler is activated by evaluating a call to the function model. The model function takes a single argument, an expression whose evaluation is to be modeled. When called, model displays a window called "Replacement Modeler", which you may move and resize just like other windows.
A complete Scheme expression will always be selected and highlighted in the Replacement Modeler window. At any point, you may do one of the following:
- Hit enter. That completely evaluates the selected expression, then rewrites the complete expression of which it is a part with the resulting value substituted.
- Hit return. That performs the next step of the evaluation of the selected expression: either its arguments are evaluated, or the function is applied to the argument values if they are already in simplest form. The complete expression is then rewritten as just described.
- Select some other part of the displayed expression by single-clicking on the corresponding left or right parenthesis.
This is easier to do than to explain, so here's an example. When the expression
(model (+ (* (- 10 7) (+ 4 1)) (- 15 (/ 12 3)) 17))
is evaluated, a window appears:
Hitting return performs one step of the evaluation, namely evaluating the arguments of +:
One may, however, wish to explore steps of the evaluation of the argument expressions. Clicking on, say, the left parenthesis of the multiplication and hitting return twice gives
The replaced expression appears in boldface to make it easy to see the correspondence between the expression and its replacement.