Teaching
211 F '05
 
Assignments
Set 1
Set 2
Set 3
Set 4
Set 5
Set 6
Set 7
Set 8
Set 9
Set 10
Set 11
Set 12
Set 13
Set 14

Problem Set 4

If you run any of the functions (as opposed to just testing them), comment out the code for running the functions before you submit your solution.

N.B.: Tests are not runs. A test is when you evaluate an expression and compare it to some expected result, while a run is when you evaluate an expression without anticipating any particular result for the expression.

Due date: 10/7 @ NOON

The goal of this problem set is to study the use of data definitions for unions of data and self-referential data definitions such as lists.

HtDP Problems:

7.2.1, 7.2.2, 9.5.4, 9.5.6, 10.1.7

Additional Problem 1:

A weather station records a temperature range (low and high) for every day. Design a data representation for daily weather reports, which consists of a date and a temperature range.

Design a function that computes the average temperature from a weather report.

Additional Problem 2:

Graphic editors manage geometric shapes such as points, lines, rectangles, circles, and so on.

Design a data representation for the class of points, lines, squares, and circles. You may assume the following about the shapes:

  1. every shape has a (solid) color
  2. every shape has an anchor point (represented as a Posn)
  3. a square's sides are always parallel to the lines of the computer canvas
Given these assumptions, make sure that your data representation records just enough to display the shapes on a canvas.

Design draw. The function consumes a shape and the dimensions of the background image. Its produces an image of the given shape on a canvas of the appropriate size.

Design in. The function consumes a Posn and a shape. It determines whether the Posn is inside of the shape.

Design translate. The function consumes a shape and a positive number (delta). It creates the same kind of shape at a position that is delta pixels to the right of the original shape.

Additional Problem 3:

Your manager wants to develop an interactive editor for numbers. Here is how far he has gotten:


;; A World is a List-of-digichars .

;; A List-of-digichars is one of: 
;; -- empty
;; -- (cons Digichar List-of-digichars)

;; A Digichar is one of:
;; -- #\0
;; -- #\1
;; -- #\2 
;; -- #\3 
;; -- #\4 
;; -- #\5 
;; -- #\6 
;; -- #\7 
;; -- #\8
;; -- #\9

Now he is stuck.

Design collect-digits. The function consumes a Keystroke and a World. If the Keystroke is a Digichar, it constructs a list from this new Char and the existing World; otherwise it just returns the given World. Hint: Chars are ordered and the function char<=? compares Chars just like < compares numbers. Then again, you can design this function without using this hint.

Add the following code to the above function:


;; --- run program run 

;; World -> Image 
;; turn the world into an image 
(define (draw w)
  (text (list->string w) 22 'red))

;; World -> World 
(define (tock w)
  (update (draw w) produce w))

(big-bang 100 100 1 empty)
(on-key-event collect-digits)
(on-tick-event tock)


Run it to see how collect-digits works in context; remember that running a function does not test it.

Design the function digi-filter, which consumes a List-of-digichars and produces a corresponding list of digits.
Example:


(digi-filter (cons #\3 (cons #\2 (cons #\1 empty))))
;; should be 
(cons 3 (cons 2 (cons 1 empty)))

Design the function value, which consumes a list of digits (see previous problem) and produces its numeric value. The first digit is the last digit for the number.
Examples:


(value (cons 3 (cons 2 (cons 1 empty))))
;; should be 
123
(value (cons 2 (cons 1 empty)))
;; should be 
12
(value (cons 1 empty))
;; should be 
1

Warning: Think back to sixth grade math, which is when you encountered the same problem.


last updated on Sat Nov 26 15:34:41 EST 2005generated with PLT Scheme