;; A Person is a structure ;; where name is their name, year is their birth year ;; and kids is a list of their children ;; (make-Person symbol num list-of-Persons) (define-struct Person (name year kids)) #| ;; Template for Person (define (f-Person a-person ...) (...(Person-name a-person)...((Person-year a-person)...(f-lop (Person-kids a-person))...))) |# ;; list-of-persons is either ;; - empty ;; - (cons Person list-of-Persons) #| ;; Template for list-of-Persons (define (f-lop a-lop ...) (cond [(empty? a-lop) ....] [(cons? a-lop) (...(f-Person (first a-lop)) (f-lop (rest a-lop))...)])) |# (define Bart (make-Person 'Bart 1979 empty)) Bart (define Lisa (make-Person 'Lisa 1981 empty)) Lisa (define Maggie (make-Person 'Maggie 1988 empty)) Maggie (define Homer (make-Person 'Homer 1955 (list Bart Lisa Maggie))) Homer (define Marge (make-Person 'Marge 1956 (list Bart Lisa Maggie))) Marge (define Selma (make-Person 'Selma 1950 empty)) Selma (define Patty (make-Person 'Patty 1950 empty)) Patty (define Jackie (make-Person 'Jackie 1926 (list Selma Patty Marge))) Jackie (define Herbert (make-Person 'Herbert 1950 empty)) Herbert (define Mona (make-Person 'Mona 1929 (list Herbert Homer))) Mona (define Abe (make-Person 'Abe 1920 (list Herbert Homer))) Abe ;;countAll: Person --> num ;; counts all the persons in the tree (define (countAll person) (+ 1 (countAll_lop (Person-kids person)))) ;;countAll_lop: list-of-persons --> num ;; counts all the persons in a list-of-persons (define (countAll_lop lop) (cond [(empty? lop) 0] [(cons? lop) (+ (countAll (first lop)) (countAll_lop (rest lop)))])) "countAll test cases:" (= 1(countAll Bart)) (= 4 (countAll Homer)) (= 7 (countAll Jackie)) ;; atLeastNKids: Person num --> list-of-sym ;; Returns a list of names of all descendents, including the person, ;; who have at least n kids. (define (atLeastNKids person n) (append (cond [(<= n (countKids (Person-kids person))) (list (Person-name person))] [else empty]) (atLeastNKids_lop (Person-kids person) n))) ;; countKids: list-of-Persons --> num ;; counts the number of Persons in the list. (define (countKids a-lop) (cond [(empty? a-lop) 0] [(cons? a-lop) (+ 1 (countKids (rest a-lop)))])) ;; atLeastNKids_lop: list-of-Persons num --> list-of-sym ;; Returns the list of all descendents, including all Persons in the list, ;; who have at least n children. (define (atLeastNKids_lop lop n) (cond [(empty? lop) empty] [(cons? lop) (append (atLeastNKids (first lop) n) (atLeastNKids_lop (rest lop) n)) ])) "atLeastNKids test cases:" (equal? (list 'Bart) (atLeastNKids Bart 0)) (equal? empty (atLeastNKids Bart 1)) (equal? (list 'Homer) (atLeastNKids Homer 1)) (equal? (list 'Jackie 'Marge) (atLeastNKids Jackie 2)) (equal? empty (atLeastNKids Jackie 4)) (equal? (list 'Homer) (atLeastNKids Abe 3)) ;; atLeastNKids2: Person num --> list-of-sym ;; Returns a list of names of all descendents, including the person, ;; who have at least N kids. (define (atLeastNKids2 person N) (atLeastNKids2_lop (Person-kids person) N N (Person-name person))) ;; atLeastNKids2_lop: list-of-Persons num num sym --> list-of-sym ;; Returns a list of names of Persons who have at least N kids. ;; The parent is included at the front of the list if the list-of-Persons ;; contains at least n Persons. (define (atLeastNKids2_lop lop N n parent) (cond [(empty? lop) (cond [(<= n 0) (cons parent empty)] [else empty])] [(cons? lop) (append (atLeastNKids2_lop (rest lop) N (sub1 n) parent) (atLeastNKids2 (first lop) N))])) "atLeastNKids2 test cases:" (equal? (list 'Bart) (atLeastNKids2 Bart 0)) (equal? empty (atLeastNKids2 Bart 1)) (equal? (list 'Homer) (atLeastNKids2 Homer 1)) (equal? (list 'Jackie 'Marge) (atLeastNKids2 Jackie 2)) (equal? empty (atLeastNKids2 Jackie 4)) (equal? (list 'Homer) (atLeastNKids2 Abe 3))