Vous êtes sur la page 1sur 3

CS 135 Winter 2009

Ian Goldberg

Assignment 3
Due Wednesday, January 28, 2009 9:30 am
Files to submit: stepping.txt, perimeter.ss, area.ss,
inversion.ss

Language level: Beginning Student.


Warmup exercises (from HtDP; not to be submitted): 8.2.1, 8.2.2, 8.6.2, 8.6.3, 6.1.1
Extra practice exercises (from HtDP; not to be submitted): 8.2.3, 8.2.4, 8.3.1, 8.3.2
Here are the assignment questions you need to submit.

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])

(b) (define low 10)


(define high 20)
(define (in-range val)
(and (>= val low) (<= val high)))
(in-range 25)

CS 135 — Winter 2009 Assignment 3 1


(c) (cond
[(or (symbol=? ’Alice ’Bob)
(= (+ 1 3) (∗ 1 3)))
(and (< 5 15) (> 2 0))]
[else (= ’Carol ’Dave)])

(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.

CS 135 — Winter 2009 Assignment 3 2


Enhancements: Reminder—enhancements are for your interest and are not to be handed in.
The textbook uses graphical examples early through the use of the draw.ss teachpack. We
have avoided this material in early lectures, because it is not purely functional (the “state” of the
canvas is changed by drawing functions through what are generally called “side effects”) and it is
hard to talk about examples and tests. We will treat similar material in the proper context (later
in the course) and the next edition of the textbook will use more functional teachpacks, such as
image.ss (for drawing images) and world.ss.
The world.ss teachpack extends the image manipulation functions of image.ss to allow sim-
ple animations. It provides functions to initialize a canvas and schedule repeated updates at the
“tick” of a clock. The updates occur through the use of an update function that you write which
consumes the current “world” and produces the next world, one tick later. The description of the
world can be as simple as one number, but typically it involves a structure containing information
about the world, and function applications to draw the picture corresponding to that information.
You can read the documentation through DrScheme’s Help Desk. We invite you to explore the
world of functional animation. The teachpack also provides a function that allows you to program
responses to graphical updates through keyboard events. Consider what you would need on top of
this to implement the basics of some of your favourite games or interactive applications. What sort
of computations do you need to describe, and what Scheme vocabulary are you lacking in order to
express those computations?
Notice that in using world.ss, you are providing a function as an argument in order to get the
updates done. Strictly speaking, this violates the rules of Beginning Student, but this is a hint as to
the power of Scheme that will be revealed in upcoming weeks.
The authors of How to Design Programs have a follow-on book called How to Design Worlds
which explores the world.ss teachpack in depth. If you are interested, you can find it at
http://world.cs.brown.edu/.

CS 135 — Winter 2009 Assignment 3 3

Vous aimerez peut-être aussi