;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PRACTICE QUIZ 1 SOLUTIONS ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; (A) ;; Data Analysis and Definition: (define-struct car-data (make year color)) ;; A car-data is a structure: ;; (make-car-data ) ;; where is a symbol ;; is a number ;; is a symbol (B) ;; Template: ;; (define (process-car-data in-car) ;; ...(car-data-make in-car)... ;; ...(car-data-year in-car)... ;; ...(car-data-color in-car)...) (C) ;; Contract: paint-job: car-data symbol --> car-data ;; Purpose: To return a new car-data structure with the ;; given new color replacing the old one. ;; Definition: ;; (define (paint-job in-car in-color) (make-car-data (car-data-make in-car) (car-data-year in-car) in-color)) ;;;;;;;;;;;;;;;;;;; ;; Data Analysis and Definitions: ;; A natural number, NN, is either: ;; 1) zero ;; 2) (add1 n) ;; where n is any natural number ;; Contract: total-odds: NN --> NN ;; Purpose: to calculate the sum of all the odd numbers ;; between the given number and zero. ;; Template: ;; (define (process-NN in-NN) ;; (cond [(zero? in-NN) ...] ;; [else ... NN ... ;; ... (process-NN (sub1 in-NN)) ...] )) ;; Definition: ;; (define (total-odds n) (cond [(zero? n) 0] [(odd? n) (+ n (total-odds (- n 1)))] [else (total-odds (- n 1))])) ;; NOTE: There are **several** ways to do this. ;; ;; If you subtract 2 every time from odd ;; numbers, your terminating condition ;; would have to check for <= 0, but that ;; doesn't really follow our natural number ;; template. ;; ;; Above is one solution that most closely ;; follows the natural number template. ;;;;;;;;;;;;;;;;; ;; Contract: eat: number --> symbol ;; Purpose: To figure out how many more doughnuts to eat ;; Definition: ;; (define (eat n) (cond [(< n 5) 'More] [(and (>= n 5) (<= n 10)) 'Stop] [else 'Sick])) ;; or this definition if we want a more precise final condition: ;; Definition: ;; (define (eat n) (cond [(< n 5) 'More] [(and (>= n 5) (<= n 10)) 'Stop] [(> n 10) 'Sick])) ;;;;;;;;;;;;;;;;;;;;; ;; Definition: (define (find-student a-name los) (cond [(empty? los) 'Student-Not-Found] [(symbol=? a-name (student-name (first los))) (first los)] [else (find-student a-name (rest los))] )) ;; NOTE: ;; ;; The problem says that you may assume the student is in ;; the list. Therefore condition #1 is not really necessary, ;; because we will never run into empty. ;; ;; In condition #2 we check the given "a-name" against the ;; first student in the list. If they are symbol=? to each ;; other, then that is the student we want to return. ;; ;; Finally, if we don't match the first name, we simply move ;; on to look through the rest of the list.