| Due date: 2/6 @ 6pm
The goal of this problem set is to practice designing data representations
using unions of classes and functions for processing unions. You must
follow the design recipe. The graders will look for data definitions,
contracts, purpose statements, examples/tests, and properly organized
function definitions.
For the latter, you must design templates. You do not need to
include the templates with your homework, however. If you do, comment
them out so the result is runnable.
--- The problem set also contains problems concerning computation rules.
HtDP Problems:
7.2.2, 7.3.1 -- Note: be careful not to clash with DrScheme's
built in definitions
Problem A1
You must visit one of the course staff at his office hours
(TA, tutor, or instructor) and ask them a specific question about
the material (this is your chance to get a bit of individual
help). Look for their office hours on the web page, they will record
your visit. [5 points]
Problem A2
Evaluate the following expressions (not the definitions) step by
step. Next to each step say whether it is (1) arithmetic (of any form),
(2) function application ("plugging in values") or (3) a conditional step.
(define (polynom x y)
(+ (* 3 (* x x)) (* y 6)))
(polynom 6 8)
(define-struct point (x y))
(define (polynom p)
(+ (* 3 (sqr (point-x p)))
(* (point-y p) 6)))
(polynom (make-point 6 8))
(define (step x)
(cond
[(< 3 x) (* 2 x)]
[else (sqr (+ x 1))]))
(step 2)
Example:
(+ (* 3 4) (- 6 (sqr 2)))
(+ 12 (- 6 (sqr 2))) ; Arithmetic (*)
(+ 12 (- 6 4)) ; Application (sqr)
(+ 12 2) ; Arithmetic (-)
14 ; Arithmetic (+)
For sanity checks, you may want to write down each step inside of
DrScheme. Since computation means calculating, you know that after
every step, the result must still produce the same answer. So if
you're ever unsure whether you're calculation is still on track, just
run the whole program and watch the same answer pop up for as many
steps as you have written down.
Problem A3
A stock brokerage firm keeps records about Comodities: Stocks and
Bonds.
- Stocks are represented with four attributes: name of the
company, stock symbol, number of shares sold, and average share
price.
- Bonds are also represented by four attributes: the name of the
company, the cost of a single Bond, the total amount outstanding,
and the quantity outstanding.
-
Design a data representation for Comodities (Stocks and Bonds).
-
Design a function, trade-made that takes a Comodity (stock
or bond) a quantity, and a total-price, and produces
an updated Comodity record. Hint: you need a formula to
calculate a new average price for a stock; update the total amount
for a bond; and update the quantity for both. You may want to
produce an error if the quatity/total-price of the trade does
not match the Bond's price.
- Show at least two tests of your function for each type of
Comodity.
Problem A4
We've seen multiple definitions for a class of Shapes. Use the
definition you made earlier (the one used for 7.3.1, found in HtDP
Section 7, including Rectangles) to complete the tasked outlined
below.
- Develop the template, fun-for-shape, which outlines
functions that consume Shapes (circles, squares, and
rectangles).
- Use the template to design move-shape-x. The function
consumes a Shape (sh) and a number (delta), and produces a shape that
is like sh but is translated by delta pixels along the
x-direction.
-
Use the template to design outside-shape. The function
consumes a Shape (sh) and a Posn (p), and determines whether p is
outside of sh (i.e., not inside the shape).
Domain Knowledge: We've seen this in class... for
a point to be within a circle, its distance to the center must be
smaller than (or equal to) the radius. For a point to be within a
rectangle, its x coordinate must be between the x coordinate of the
left and right. Naturally something analogous holds for the y
coordinates. Remember that squares are just special rectangles.
Don't forget to use 'center' and 'nw' (top left) in your
calculations!
-
Use the template to design draw-shape. The function consumes
a Shape (sh) and a Scene (sc) and adds (a representation of) sh to sc.
Domain Knowledge: Remember, the documentation of
world.ss specifies how to add (place) an image into a Scene.
Hint: most of the exercises do not depend on each other. If
you're stuck with one, try another one. Also, watch out for any
names clashes with DrScheme defined functions and variables.
|