;; 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-reader.ss" "lang")((modname week3_tu_full) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; 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)) ; a list-of-numbers is: ; empty, or ; (cons number list-of-numbers) ; example: (define one-to-five (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 empty))))) ) ; 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)