Index: Recursive examples, Ping-pong example (continued)
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 23End 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.
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.
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:
(define (move-in-box ball t) (cond [(eq? (bounces-off ball t) 'NORTH-SOUTH) (move-in-box (ns-bounce (move ball (ns-time-to-wall ball))))] [(eq? (bounces-off-ball t) 'EAST-WEST) (move-in-box (ew-bounce (move ball (ew-time-to-wall ball))))] [else (move ball t)]))and test with (trace-ball a-ball ball-posn move-in-box a-time).