;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-abbr-reader.ss" "lang")((modname classroomsandsorting) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ; a student is a ; - symbol name ; - number iq ; - boolean graduate? (define-struct student (name iq graduate?)) (define student-a (make-student 'andrea 140 true)) (define student-b (make-student 'andrea 154 true)) ;Q1: what does the template for students look like? ;(template for students: ; ( ... (student-name s) ... ; ... (student-iq s) ... ; ... (student-graduate? s) ...) ;Q2: design and write the teach function, which increases student intelligence by 10% ; teach: student->student ; increases student intelligence by 10% (define (teach s) (make-student (student-name s) (* (student-iq s) 1.1) (student-graduate? s)) ) (check-expect (teach student-a) student-b) ; a professor is a ; - number iq (define-struct professor (iq)) ; a ta is a ; - number iq ; - symbol office-building (define-struct ta (iq office-building)) ; a classroom-denizen is either a: ; - student, or ; - professor, or ; - ta ; template: ; (cond [(student? c) ... (fun-for-student c) ...] ; [(professor? c) ... (fun-for-professor c) ... ] ; [(ta? c) ... (fun-for-ta c) ... ]) ; basic template rules: ; when you hit conditional data, write a cond for each option ; when you hit compound data, write out the selectors ; when you hit a *defined* data type, use a helpful function ; can write "fun-for-x" to make this explicit ;Q3: Consider the watch-tv function which decreases the intelligence of professors and tas by 5%, but has no effect on student intelligence. ; Design and write this function, using appropriate helper functions where needed (*don't* define these). ; watch-tv: classroom-denizen -> classroom-denizen ; returns the results of watching TV for the classroom denizen (define (watch-tv c) (cond [(student? c) (student-watch-tv c)] [(professor? c) (professor-watch-tv c)] [(ta? c) (ta-watch-tv c)]) ) ;quick helper function definitions with no design recipe :( : (define (student-watch-tv c) c) (define (ta-watch-tv c) (make-ta (* (ta-iq c) 0.95) (ta-office-building c))) (define (professor-watch-tv c) (make-professor (* (professor-iq c) 0.95))) (check-expect (watch-tv student-a) student-a) (check-expect (watch-tv student-b) student-b) (check-expect (watch-tv (make-ta 120 'ford)) (make-ta 114 'ford)) (check-expect (watch-tv (make-professor 100)) (make-professor 95)) ; example: (define one-to-five (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty))))) ) ; a list-of-numbers is: ; empty, or ; (cons number list-of-numbers) ; count-odd-numbers: list-of-numbers -> number ; returns the numbers of odd numbers in the given list ; template: ; (define (fun-for-list-of-numbers numlist) ; (cond [(empty? numlist) ...] ; [(cons? numlist) ... (first numlist) ... (fun-for-list-of-numbers (rest numlist)) ... ]) (define (count-odd-numbers numlist) (cond [(empty? numlist) 0] [(cons? numlist) (+ (cond [(odd? (first numlist)) 1] [else 0] ) (count-odd-numbers (rest numlist)))])) (check-expect (count-odd-numbers one-to-five) 3) (check-expect (count-odd-numbers empty) 0) (check-expect (count-odd-numbers (cons 2 empty)) 0) ; a classroom is ; - empty ; - (cons classroom-denizen classroom) ; (define (fun-for-classroom cr) ; (cond [(empty? cr) ...] ; [(cons? cr) ... (first cr) ... (fun-for-classroom (rest cr)) ... ]) ;Q4: write the "everyone-watches-TV" function, which updates an intelligence of a classroom after everyone watches tv ; everyone-watches-tv: classroom -> classroom ; Return the result of having everyone in a classroom ; watch tv (define (everyone-watches-tv cr) (cond [(empty? cr) empty] [(cons? cr) (cons (watch-tv (first cr)) (everyone-watches-tv (rest cr)))])) (check-expect (everyone-watches-tv (cons (make-ta 120 'ford) (cons (make-professor 100) (cons (make-student 'frank 100 true) empty))) ) (cons (make-ta 114 'ford) (cons (make-professor 95) (cons (make-student 'frank 100 true) empty)))) ; (define (fun-for-list-of-numbers numlist) ; (cond [(empty? numlist) ...] ; [(cons? numlist) ... (first numlist) ... (fun-for-list-of-numbers (rest numlist)) ... ]) ; sort: list-of-numbers -> list-of-numbers ; sorts the provided list in ascending order (define (sort numlist) (cond [(empty? numlist) empty] [(cons? numlist) (insert (first numlist) (sort (rest numlist))) ]) ) (check-expect (sort (cons 2 (cons 1 empty))) (cons 1 (cons 2 empty))) (check-expect (sort empty) empty) ; insert: number list-of-numbers -> list-of-numbers ; inserts number into list, assuming list is sorted ; (ascending order) (define (insert n numlist) (cond [(empty? numlist) (cons n empty)] [(cons? numlist) (cond [(< n (first numlist)) (cons n numlist)] [(> n (first numlist)) (cons (first numlist) (insert n (rest numlist)))] [(= n (first numlist)) (cons n numlist)])]) ) (check-expect (insert 2 empty) (cons 2 empty)) (check-expect (insert 2 (cons 3 empty)) (cons 2 (cons 3 empty))) (check-expect (insert 2 (cons 1 empty)) (cons 1 (cons 2 empty)))