(define-struct child (name father mother year eyes)) (define C-Adam (make-child 'Adam empty empty 1900 'blue)) (define C-Al (make-child 'Al empty empty 1910 'green)) (define C-Ada (make-child 'Ada empty empty 1905 'green)) (define C-Amy (make-child 'Amy empty empty 1900 'brown)) (define C-Betty (make-child 'Betty C-Adam C-Ada 1930 'blue)) (define C-Bill (make-child 'Bill C-Adam C-Ada 1931 'blue)) (define C-Bess (make-child 'Bess C-Adam C-Ada 1932 'brown)) (define C-Bob (make-child 'Bob C-Al C-Amy 1940 'green)) (define C-Beth (make-child 'Beth C-Al C-Amy 1942 'grey)) (define C-Cathy (make-child 'Cathy C-Bob C-Betty 1950 'green)) (define C-Chuck (make-child 'Chuck C-Bob C-Betty 1952 'blue)) (define C-Candy (make-child 'Candy C-Bob C-Beth 1960 'grey)) (define C-Chris (make-child 'Chris C-Bill C-Bess 1960 'brown)) (define sample-loftn (list C-Cathy C-Chuck C-Candy C-Chris)) ; parents-of-helper : ftn symbol -> list-of-ftn ; Returns the two parents of the given person. If none found, returns empty. (define (C-parents-of-helper a-ftn name) (cond [(empty? a-ftn) empty] [(child? a-ftn) (cond [(symbol=? (child-name a-ftn) name) (list (child-father a-ftn) (child-mother a-ftn))] [(not (empty? (C-parents-of-helper (child-father a-ftn) name))) (C-parents-of-helper (child-father a-ftn) name)] [else (C-parents-of-helper (child-mother a-ftn) name)])])) ; C-parents-of : list-of-ftn symbol -> list-of-ftn ; Returns the two parents of the given person. If none found, returns empty. (define (C-parents-of a-loftn name) (cond [(empty? a-loftn) empty] [(cons? a-loftn) (cond [(empty? (C-parents-of-helper (first a-loftn) name)) (C-parents-of (rest a-loftn) name)] [else (C-parents-of-helper (first a-loftn) name)])])) ; C-same-person? ftn ftn -> boolean ; Return whether the two trees represent the same person. ; Unknown people (empty trees) are assumed to be different. ; Assumes all names are distinct. (define (C-same-person? ftn1 ftn2) (cond [(or (empty? ftn1) (empty? ftn2)) false] [(and (child? ftn1) (child? ftn2)) (symbol=? (child-name ftn1) (child-name ftn2))])) ; C-find-children-helper ftn ftn ftn -> list-of-ftn ; Assumes no biologically impossible family relationships. ; Return list of all children in the family tree having the given father and mother. (define (C-find-children-helper a-ftn father mother) (cond [(empty? a-ftn) empty] [(child? a-ftn) (cond [(and (C-same-person? (child-father a-ftn) father) (C-same-person? (child-mother a-ftn) mother)) (list a-ftn)] [else (append (C-find-children-helper (child-father a-ftn) father mother) (C-find-children-helper (child-mother a-ftn) father mother))])])) ; C-find-children list-of-ftn ftn ftn -> list-of-ftn ; Return list of all children in the list of family trees having the given father and mother. (define (C-find-children a-loftn father mother) (cond [(empty? a-loftn) empty] [(cons? a-loftn) (append (C-find-children-helper (first a-loftn) father mother) (C-find-children (rest a-loftn) father mother))])) ; C-remove-by-name : list-of-ftn symbol -> list-of-ftn ; Return a list of people like the original, but without the named person. (define (C-remove-by-name a-loftn name) (cond [(empty? a-loftn) empty] [(cons? a-loftn) (cond [(symbol=? (child-name (first a-loftn)) name) (C-remove-by-name (rest a-loftn) name)] [else (cons (first a-loftn) (C-remove-by-name (rest a-loftn) name))])])) ; C-find-siblings : list-of-ftn symbol -> list-of-ftn ; Return list of all siblings of named person. ; May include duplicate entries. (define (C-find-siblings a-loftn name) (cond [(empty? a-loftn) empty] [(cons? a-loftn) (cond [(empty? (C-parents-of a-loftn name)) empty] [else (C-remove-by-name (C-find-children a-loftn (first (C-parents-of a-loftn name)) (first (rest (C-parents-of a-loftn name)))) name)])])) (C-find-siblings sample-loftn 'Chris) = empty (C-find-siblings sample-loftn 'Bill) = (list C-Betty C-Bess) (C-find-siblings sample-loftn 'Bob) = (list C-Beth) (C-find-siblings sample-loftn 'Chuck) = (list C-Cathy) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-struct parent (name year eyes children)) (define P-Cathy (make-parent 'Cathy 1950 'green empty)) (define P-Chuck (make-parent 'Chuck 1952 'blue empty)) (define P-Candy (make-parent 'Candy 1960 'grey empty)) (define P-Chris (make-parent 'Chris 1960 'brown empty)) (define P-Betty (make-parent 'Betty 1930 'blue (list P-Cathy P-Chuck))) (define P-Bill (make-parent 'Bill 1931 'blue (list P-Chris))) (define P-Bess (make-parent 'Bess 1932 'brown (list P-Chris))) (define P-Bob (make-parent 'Bob 1940 'green (list P-Cathy P-Chuck))) (define P-Beth (make-parent 'Beth 1942 'grey (list P-Candy))) (define P-Adam (make-parent 'Adam 1900 'blue (list P-Betty P-Bill P-Bess))) (define P-Al (make-parent 'Al 1910 'green (list P-Bob P-Beth))) (define P-Ada (make-parent 'Ada 1905 'green (list P-Betty P-Bill P-Bess P-Bob))) (define P-Amy (make-parent 'Amy 1900 'brown (list P-Bob P-Beth))) (define sample-lop (list P-Adam P-Al P-Ada P-Amy)) ; P-same-person? : parent symbol -> boolean ; Returns whether the parent has the given name. (define (P-same-person? a-parent name) (symbol=? (parent-name a-parent) name)) ; P-child-in-list? : list-of-parent symbol -> boolean ; Returns whether the named person is in the given list. (define (P-child-in-list? a-lop name) (cond [(empty? a-lop) false] [(cons? a-lop) (or (P-same-person? (first a-lop) name) (P-child-in-list? (rest a-lop) name))])) ; P-remove-by-name : list-of-parent symbol -> list-of-parent ; Returns a list of people like the original, but without the named person. ; Assumes each person is distinct. (define (P-remove-by-name a-lop name) (cond [(empty? a-lop) empty] [(cons? a-lop) (cond [(P-same-person? (first a-lop) name) (rest a-lop)] [else (cons (first a-lop) (P-remove-by-name (rest a-lop) name))])])) ; P-find-siblings : list-of-parent symbol -> list-of-ftn ; Returns a list of all siblings of the named person. (define (P-find-siblings a-lop name) (cond [(empty? a-lop) empty] [(cons? a-lop) (cond [(empty? (P-find-siblings-parent (first a-lop) name)) (P-find-siblings (rest a-lop) name)] [else (P-find-siblings-parent (first a-lop) name)])])) ; P-find-siblings-parent : parent symbol -> list-of-ftn ; Returns a list of all siblings of the named person. (define (P-find-siblings-parent a-parent name) (cond [(P-child-in-list? (parent-children a-parent) name) (P-remove-by-name (parent-children a-parent) name)] [else (P-find-siblings (parent-children a-parent) name)])) ;(P-find-siblings sample-lop 'Chris) = empty ;(P-find-siblings sample-lop 'Bill) = (list P-Betty P-Bess) ;(P-find-siblings sample-lop 'Bob) = (list P-Beth) ;(P-find-siblings sample-lop 'Chuck) = (list P-Cathy)