Académique Documents
Professionnel Documents
Culture Documents
Ian Goldberg
Assignment 3
Due Wednesday, January 28, 2009 9:30 am
Files to submit: stepping.txt, perimeter.ss, area.ss,
inversion.ss
1. In this question, you will practice step-by-step evaluations of Scheme programs. You should
not use DrScheme’s Stepper to help you with this question, for a couple of reasons. First,
as mentioned in class, DrScheme’s evaluation rules are slightly different from the ones pre-
sented in class; you are to use the ones presented in class. Second, in an exam situation,
of course, you will not have the Stepper to help you. There will definitely be step-by-step
evaluation questions on at least the first midterm.
Using the semantics given in class for Beginning Student Scheme, show the complete step-
by-step evaluation for each of the following Scheme programs. When you can no longer
perform any substitutions according to our semantic model, explain why. Place your solu-
tions in a single plain text file (not a .ss file) called stepping.txt.
(a) (define x 3)
(define y (+ x 2))
(cond
[(> x 4) z]
[(> x 2) y]
[else x])
(d) (define (f x)
(cond
[(even? x) (+ 1 (f (/ x 2)))]
[else 0]))
(f 20)
2. Write a function triangle-perimeter that consumes three posns representing the vertices of a
triangle, and produces the perimeter of the triangle. (Hint: You can use the formula for dis-
tance between two points as given in class.) Place your answer in the file perimeter.ss.
3. Write a function triangle-area that consumes three posns representing the vertices of a tri-
angle, and produces the area of the triangle. If the vertices are (x1 , y1 ), (x2 , y2 ), and (x3 , y3 ),
the area is given by 12 |x1 y2 − x2 y1 + x2 y3 − x3 y2 + x3 y1 − x1 y3 |. (Note the absolute value
sign.) Place your answer in the file area.ss.
B
4. This problem concerns the geometric concept of an inver-
sion. We will consider the simplest case, that of inversion A
in the unit circle (the circle centred at the origin O, and
with radius 1). Point B is the inversion of point A if O, A,
and B are on the same straight line, A and B are on the O 1
same side of O, and OA · OB = 1. OA is the distance
from O to A, and similarly for OB. The point O itself has
no inversion.
Write a function unit-inversion which consumes a posn
representing the coordinates of a point, and produces the
posn which represents the coordinates of the inversion of
that point. (Hint: your function may be similar to the scale function seen in class, but the
amount to scale by will depend on the point’s distance from the origin.) Place your answer
in the file inversion.ss.
Things you might think about (but don’t have to hand in):
given a posn p, what is (unit-inversion (unit-inversion p))?
For what points p is (unit-inversion p) equal to p?
This concludes the list of questions for which you need to submit solutions. Don’t forget to always
request a public test after making a submission.