Comp 210 Lab 3: More Data Structures

Index: Recursive examples, Ping-pong example (continued)


Recursive examples

Average

To do: Write a program which takes a list of numbers and returns the average of all the numbers. Follow all steps of the design recipe. What should it return if the list is empty? (We'll define it to be #f.)

For the curious... To do: Save (as text!) your solution, using the filename average.ss. Copy the file average into the same directory. Run (from an xterm window) the program average, e.g.,

     % average
     10
     -5
     31
     0
     23
End the input with Control-d. This calls the program you wrote in Scheme. This just illustrates that, even though we're using DrScheme, Scheme programs can be run as "stand-alone" programs.

A Database

To do: First, copy the following into a file db.ss.

     ;; Some basic constants: 
     (define MAX-SALARY 1000000)
     (define MIN-SALARY   20000)

     (define MIN-AGE 18)
     (define MAX-AGE 65)

     ;; db : (listof record)

     ;; records: 
     (define-struct record (name age salary))

     ;; A db record is (make-record symbol a s)
     ;;   where a is an integer between MIN-AGE and MAX-AGE (incl)
     ;;   and s is an integer between MIN-SALARY and MAX-SALARY (incl)
Now write db-search, which takes a db and returns those employees who are older than 22 and earn more than 100,000 (in the order they appear in the db). Follow all steps of the design recipe.

For the curious... To do: Save (as text!) your solution, using the filename db.ss. Copy the file db into the same directory. Run (from an xterm window) the program db, e.g.,

     % db
     Man    23 120000
     BigMan 46 800000
     Little 19  30000
     Woman  32 300000
     Kiddo  18  20000

For the curious... To do: Write create-record : any any any -> record which either uses make-record or error as appropriate, based on its arguments.


Ping-pong example (continued)

Let's start lab by picking up where we left off with ping-pong. Here's solutions to the first two groups of exercises First, we model straight-line movement:

     (define-struct vec (x y))

     ;; posn+ : posn vec -> posn
     (define (posn+ aposn avec)
        (make-posn (+ (posn-x aposn) (vec-x avec))
                   (+ (posn-y aposn) (vec-y avec))))

     ;; vec* : number vec -> vec
     (define (vec* anum avec)
        (make-vec (* anum (vec-x avec))
	          (* anum (vec-y avec))))

     ;; move : posn vec number -> posn
     (define (move aposn avec anum)
        (posn+ aposn (vec* anum avec)))
Then, we redo straight-line movement by introducing a ball data structure:
     (define-struct ball (posn speed))

     ;; move : ball number -> ball
     (define (move aball anum)
        (make-ball (posn+ (ball-posn ball) (vec* anum (ball-speed ball)))
	           (ball-speed ball)))
Make sure you understand these definitions before continuing.

To do: Form pairs of students. Do the exercises in section 5.4. You won't finish all these exercises during lab, but hopefully you'll be interested enough to finish on your own. Warning: The online version is garbled in places, so here's an outline:

  1. Model bouncing off a wall:
    1. Write east-bounce : ball -> ball, and corresponding functions west-bounce, north-bounce, and south-bounce.
    2. Modify these similar functions to write the corresponding functions ew-bounce and ns-bounce.
    3. Write ns-speed : ball -> number and the corresponding function ew-speed.
    4. Write ns-direction : ball -> symbol and the corresponding function ew-direction. The former returns either 'NORTH or 'SOUTH; the latter, either 'EAST or 'WEST.
    5. Write ns-time-to-wall : ball -> number and the corresponding function ew-time-to-wall. Assume the ball does not have a speed of zero in either direction.
  2. Model bouncing with four walls, no paddle:
  3. Model bouncing off walls and a paddle: