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 3

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: 9/26 @ NOON

Use this problem set to practice processing structured forms of data.

HtDP Problems:

6.3.1 (2,4), 6.4.1 (2,4), 6.5.1 (2,4), 6.4.2

Additional Problem 1:

Your mail advertising firm has a database on people. The database access function delivers records according to the following data definition:


(define-struct db
 (last first middle prefix street 
  no city state zip priority))
;; A db record (DBR) is: 
;;  (make-db
;;    String String Char String String 
;;    Number String State Zip Number))
;; A State is a String such "AZ" ... "WY".
;; A Zip is a String such as 01000 or 09999. 

Design the function create-address, which consumes a DBR and produces a three-line address string. Hint: "\n" is a string that ends a line when printed on a computer monitor or on paper.

Additional Problem 2:

Here is a data definition for keeping track of time on a clock (which we know is a computer with a display attached to it):

(define-struct time (hours minutes))
;; Time = (make-time Number Number)
;; Constraints: 
;; The first number is always between 0 and 11; 
;; the second number is always between 0 and 59. 

Design the function tick, which adds one minute to the given time.

Design the function time->string, which converts a time to a string like a clock. For example, when given (make-time 1 6) produces "01:06".

If you fill in the dots in the following code snippet, running it displays a clock that adds a minute per second:


;; Time -> Image 
;; convert time to text image with 
;; font size 22 and color 'red 
(define (draw t) ...)

;; --- run program run ---
;; Time -> Time 
(define (tock t) (update (draw t) produce (tick t)))

 ;; how far from the left the clock time is displayed
(define X-OFFSET 
  5)

;; the width of the display (just enough for a clock time)
(define TWIDTH 
  (+ (image-width (text "00:00" 20 'red)) X-OFFSET))

(big-bang TWIDTH 30 1 (make-time 0 0))

(on-tick-event tock)

Do not attempt this before you have finished the design of the functions.

Optional: Modify the data definition so that Time also keeps track of seconds. Then modify tick. Modify means copy all functions that you wish to modify and add ".v1" to the end of the function name. That way you can turn in the program proper and the optional functions in one definitions window.

Additional Problem 3:

Study these structure and data definitions:


(define-struct gallery (one two three))
;; Gallery = (make-gallery Image Image Image)

The goal is to design functions that can deal with a gallery like this now and later with a gallery of arbitrarily many pictures.

Find your three favorite images on-line (not of same size) and create a gallery from them in DrScheme.

Design image-pad. The function consumes an image and two numbers (width, height). It produces an image with a white frame so that the resulting image is of size width x height.

Design the function image-compose, which consumes two images and juxtaposes them next to each other. Make sure that the upper right corner of the first image coincides with the upper left corner of the second image.

Design the function gallery-pad, which pads each image in the given gallery so that all of them have the same width and height afterwards.

Design the function gallery-draw, which turn the images in a gallery into a single image after padding the gallery first.

Design the function rotate, which consumes and creates a gallery and by shifting all images to the right. The last image becomes the first.

Design the function click, which consumes a keystroke and a gallery. If the keystroke is a right arrow, the resulting gallery is rotated once; if it is a left arrow, the resulting gallery is rotated twice; otherwise the function returns the given gallery.

Putting everything together yields a program for "walking" through the gallery:


;; --- run program run 
;; World = Gallery

;; World -> World
(define (tock w)
  (update (gallery-draw w) produce w))
(big-bang 100 30 1 gallery0)
(on-tick-event tock)
(on-key-event click)

Run the program. Remember running a program is not testing it.

Optional Problem 4:

Do not turn in a solution to this problem. If you would have feedback on your solution, meet with a tutor, a TA, or a professor and discuss your code in person.

Your manager asked you to develop a portion of a text display manager (TDM). The TDM notices the user's keystrokes and adds them to a display. For example, the display looks like this:

if the user has pressed the keys a, b, and c. Naturally, your manager suggests to represent the current state of the world as a string (all the character keys pressed so far):

;; TeachPack: world.ss

;; World = String 
;; interpretation: the word typed in so far 

For example, the World for the above is "abc".

Design the function next, which consumes a World and a Keystroke. Remember from the teachpack documentation in HelpDesk that


;; A Keystroke is either:
;; -- a Char (character)
;; -- a Symbol

Also, Chars are noted as #\a, #\b, #\5, etc.

If the Keystroke is a character (char?), next appends it to the end of the World; otherwise it is omitted. Hint: The function make-string consumes a number (e.g., 2) and a character (#\a) and produces a string of that many chars ("aa").

Design the function draw, which consumes a string and produces a text image using font size 20 and font color red at the left end of an empty scene of 100 x 30 pixels.

Add the following code fragment at the bottom of your program and fill in the dots to get your primitive "text editor" working:


;; --- run program run ---

;; World -> World
;; on every tick of the program, ... 
(define (tock w) (update (draw w) produce w))

;; Keystroke World -> World 
(define (click key w) ...)

(big-bang 100 30 1 "")
(on-tick-event tock) 
(on-key-event click)

Do not attempt this before you have finished the design of the functions.

Optional: Change the program so that when the user presses the backspace key (#\backspace), no new character appears and the previously typed character disappears. Hints: (1) Design the function all-but-last, which consume a string. Its result is the same string but with the last character removed. For example, when given "hello", the function produces "hell". (2) Look up the function substring in HelpDesk and play with it in the Interactions Window.

Optional: Change the program so that it only deals with a character if the resulting text still fits into the frame.

If you choose to turn in the optional parts, turn in both the original program and the modifications. You will get feedback on optional problems but no points.


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