;; 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) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; data definitions. ; a students is a: ; - (make-student symbol number boolean) (define-struct student (name iq graduate?)) ; behind the scenes, Racket creates functions: ; (student-name ...) ; (student-iq ...) ; (student-graduate? ...) ; (make-student ...) ; (student? ...) (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? ;(define (fun-for-student s) ; ...(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) (* 1.1 (student-iq s)) (student-graduate? s)) ) (check-expect (teach student-a) student-b) ; Q3: Consider the watch-tv function which decreases intelligence by 5% for professors and tas, but has no effect on student intelligence. Design and write this function, using appropriate helper functions where needed. ; a professors is: ; - (make-professor number) (define-struct professor (iq)) ; a ta is a: ; - (make-ta number symbol) (define-struct ta (iq office-building)) ; a classroom-denizen is either a: ; student, or ; professor, or ; ta ; template: ; (define (fun-for-classroom-denizen c) ; (cond [(student? c) (fun-for-student c) ] ; [(professor? c) (fun-for-professor c) ] ; [(ta? c) (fun-for-ta c) ])) ;remember basic template rules: ; conditional data -> write a cond for each option ; compound data -> write out selectors ; when you hit a *defined* data structure, us a helper function ; can write (fun-for-x ...) to make this explicit ; watch-tv : classroom-denizen -> classroom-denizen ; decreases intelligence for professors and tas (not students) by 5% ;(define (watch-tv c) ; (cond [(student? c) c ] ; [(professor? c) (decrease-professor-iq c) ] ; [(ta? c) (decrease-ta-iq c) ])) ;(check-expect (watch-tv student-a) student-a) ;(check-expect (watch-tv (make-ta 120 'ford)) (make-ta 114 'ford)) ;(check-expect (watch-tv (make-professor 100)) (make-professor 95)) ; would still need to write helper functions ; cons! ; a list-of-numbers is: ; - empty, or ; - (cons number list-of-numbers) ; (define (fun-for-list-of-numbers lon) ; (cond [(empty? lon) ...] ; [(cons? lon) ...(first lon) ...(fun-for-lon (rest lon))...]) ; ) ;example: (define one-to-four (cons 1 (cons 2 (cons 3 (cons 4 empty) ) ) ) ) ; count-odd-numbers : list-of-number -> number ; return the number of odd numbers in the given list (define (count-odd-numbers lon) (cond [(empty? lon) 0] [(cons? lon) (+ (cond [(odd? (first lon)) 1] [else 0]) (count-odd-numbers (rest lon)))]) ) (check-expect (count-odd-numbers one-to-four) 2) (check-expect (count-odd-numbers empty) 0)