(define-struct person (name year eye children)) ;; a Person is a (make-person String Natural String ListOfPerson) ;; interp: a person with ;; name as the person's name ;; year is the year of birth ;; eye is the eye color ;; children is a list of the person's children ;; a ListOfPerson is one of ;; empty ;; (cons Person ListOfPerson) (define SusanTree (make-person "Susan" 1920 "blue" (list (make-person "Joe" 1938 "green" empty) (make-person "Helen" 1940 "brown" (list (make-person "Hank" 1965 "green" empty) (make-person "Cara" 1969 "brown" empty))) (make-person "Ricky" 1942 "blue" empty)))) ;;; templates ; ;;; fcn-for-person: Person -> ;;; ;(define (fcn-for-person aper) ; (... (person-name aper) ; (person-year aper) ; (person-eye aper) ; (fcn-for-lop (person-children aper)))) ; ;;; fcn-for-lop: ListOfPerson -> ;;; ;(define (fcn-for-lop alop) ; (cond [(empty? alop) ] ; [(cons? alop) (...(fcn-for-person (first alop)) ; (fcn-for-lop (rest alop)))])) ;;has-teen-or-child? Person -> Boolean ;; produces true if anyone in the person's family tree is born after 1994 (define (has-teen-or-child? aper) (or (> (person-year aper) 1994) (child-in-list? (person-children aper)))) ;; child-in-list?: ListOfPerson -> Boolean ;; produces true if anyone in the list (or any of their descendants) is born after 1994 (define (child-in-list? alop) (cond [(empty? alop) false ] [(cons? alop) (or (has-teen-or-child? (first alop)) (child-in-list? (rest alop)))])) (check-expect (has-teen-or-child? SusanTree) false) (check-expect (has-teen-or-child? (make-person "Susan" 1920 "blue" (list (make-person "Joe" 1938 "green" empty) (make-person "Helen" 1940 "brown" (list (make-person "Hank" 1965 "green" empty) (make-person "Cara" 1999 "brown" empty))) (make-person "Ricky" 1942 "blue" empty)))) true) ;; count-older-blue: Person Natural -> Natural ;; produces the number of blue-eyed people in the tree who were born before the given year (define (count-older-blue aper year) (+ (if (and (< (person-year aper) year) (string=? (person-eye aper) "blue")) 1 0) (count-older-blue-lop (person-children aper) year))) ;; count-older-blue-lop: ListOfPerson -> Natural ;; produces the number of blue-eyed people from the list (and any of their descendants) who ;; were born before the given year (define (count-older-blue-lop alop year) (cond [(empty? alop) 0 ] [(cons? alop) (+ (count-older-blue (first alop) year) (count-older-blue-lop (rest alop) year))])) (check-expect (count-older-blue SusanTree 1950) 2)