Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 14.1.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;A child structure is
5 12687dd9 2023-08-04 jrmu ;(make-child father mother name date eyes) where
6 12687dd9 2023-08-04 jrmu ;
7 12687dd9 2023-08-04 jrmu ;1. name and eyes are symbols,
8 12687dd9 2023-08-04 jrmu ;2. date is a number,
9 12687dd9 2023-08-04 jrmu ;3. and father and mother are either
10 12687dd9 2023-08-04 jrmu ;a. child structures or
11 12687dd9 2023-08-04 jrmu ;b. empty.
12 12687dd9 2023-08-04 jrmu ;
13 12687dd9 2023-08-04 jrmu (define-struct child (father mother name date eyes))
14 12687dd9 2023-08-04 jrmu ;
15 12687dd9 2023-08-04 jrmu ;A family tree node (ftn) is either
16 12687dd9 2023-08-04 jrmu ;1. empty or
17 12687dd9 2023-08-04 jrmu ;2. (make-child father mother name date eyes) where
18 12687dd9 2023-08-04 jrmu ;a. father and mother are ftn,
19 12687dd9 2023-08-04 jrmu ;b. name and eyes are symbols,
20 12687dd9 2023-08-04 jrmu ;c. and date is a number.
21 12687dd9 2023-08-04 jrmu ;
22 12687dd9 2023-08-04 jrmu
23 12687dd9 2023-08-04 jrmu (define Carl (make-child empty empty 'Carl 1926 'green))
24 12687dd9 2023-08-04 jrmu (define Bettina (make-child empty empty 'Bettina 1926 'green))
25 12687dd9 2023-08-04 jrmu (define Adam (make-child Carl Bettina 'Adam 1950 'yellow))
26 12687dd9 2023-08-04 jrmu (define Dave (make-child Carl Bettina 'Dave 1955 'black))
27 12687dd9 2023-08-04 jrmu (define Eva (make-child Carl Bettina 'Eva 1965 'blue))
28 12687dd9 2023-08-04 jrmu (define Fred (make-child empty empty 'Fred 1966 'pink))
29 12687dd9 2023-08-04 jrmu (define Gustav (make-child Fred Eva 'Gustav 1988 'brown))
30 12687dd9 2023-08-04 jrmu
31 12687dd9 2023-08-04 jrmu ;Template
32 12687dd9 2023-08-04 jrmu ;fun-for-ftn : ftn -> ???
33 12687dd9 2023-08-04 jrmu ;(define (fun-for-ftn a-ftree)
34 12687dd9 2023-08-04 jrmu ; (cond
35 12687dd9 2023-08-04 jrmu ; [(empty? a-ftree) ...]
36 12687dd9 2023-08-04 jrmu ; [else
37 12687dd9 2023-08-04 jrmu ; ... (fun-for-ftn (child-father a-ftree)) ...
38 12687dd9 2023-08-04 jrmu ; ... (fun-for-ftn (child-mother a-ftree)) ...
39 12687dd9 2023-08-04 jrmu ; ... (child-name a-ftree) ...
40 12687dd9 2023-08-04 jrmu ; ... (child-date a-ftree) ...
41 12687dd9 2023-08-04 jrmu ; ... (child-eyes a-ftree) ...]))
42 12687dd9 2023-08-04 jrmu
43 12687dd9 2023-08-04 jrmu ;blue-eyed-ancestor? : ftn -> boolean
44 12687dd9 2023-08-04 jrmu ;Given a-ftree, determine if there is
45 12687dd9 2023-08-04 jrmu ;a child in a-ftree with 'blue in eyes field.
46 12687dd9 2023-08-04 jrmu (define (blue-eyed-ancestor? a-ftree)
47 12687dd9 2023-08-04 jrmu (cond
48 12687dd9 2023-08-04 jrmu [(empty? a-ftree) false]
49 12687dd9 2023-08-04 jrmu [else
50 12687dd9 2023-08-04 jrmu (or
51 12687dd9 2023-08-04 jrmu (symbol=? (child-eyes a-ftree) 'blue)
52 12687dd9 2023-08-04 jrmu (blue-eyed-ancestor? (child-father a-ftree))
53 12687dd9 2023-08-04 jrmu (blue-eyed-ancestor? (child-mother a-ftree)))]))
54 12687dd9 2023-08-04 jrmu
55 12687dd9 2023-08-04 jrmu ;count-persons : ftn -> number
56 12687dd9 2023-08-04 jrmu ;Given a-ftree, determines the number of people in the
57 12687dd9 2023-08-04 jrmu ;family tree.
58 12687dd9 2023-08-04 jrmu
59 12687dd9 2023-08-04 jrmu ;Examples:
60 12687dd9 2023-08-04 jrmu ;(count-persons Carl)
61 12687dd9 2023-08-04 jrmu ;1
62 12687dd9 2023-08-04 jrmu ;(count-persons Dave)
63 12687dd9 2023-08-04 jrmu ;3
64 12687dd9 2023-08-04 jrmu
65 12687dd9 2023-08-04 jrmu (define (count-persons a-ftree)
66 12687dd9 2023-08-04 jrmu (cond
67 12687dd9 2023-08-04 jrmu [(empty? a-ftree) 0]
68 12687dd9 2023-08-04 jrmu [else
69 12687dd9 2023-08-04 jrmu (+ 1
70 12687dd9 2023-08-04 jrmu (count-persons (child-father a-ftree))
71 12687dd9 2023-08-04 jrmu (count-persons (child-mother a-ftree)))]))
72 12687dd9 2023-08-04 jrmu ;
73 12687dd9 2023-08-04 jrmu ;average-age : ftn -> number
74 12687dd9 2023-08-04 jrmu ;Computes the average age of all the people
75 12687dd9 2023-08-04 jrmu ;in a family tree. Sums the ages
76 12687dd9 2023-08-04 jrmu ;and divides by the count of persons.
77 12687dd9 2023-08-04 jrmu
78 12687dd9 2023-08-04 jrmu (define (average-age a-ftree)
79 12687dd9 2023-08-04 jrmu (/ (sum-of-ages a-ftree)
80 12687dd9 2023-08-04 jrmu (count-persons a-ftree)))
81 12687dd9 2023-08-04 jrmu ;
82 12687dd9 2023-08-04 jrmu ;sum-of-ages : ftn -> number
83 12687dd9 2023-08-04 jrmu ;Computes the sum of the ages of
84 12687dd9 2023-08-04 jrmu ;the people in a family tree.
85 12687dd9 2023-08-04 jrmu ;
86 12687dd9 2023-08-04 jrmu ;Examples:
87 12687dd9 2023-08-04 jrmu ;(sum-of-ages Carl)
88 12687dd9 2023-08-04 jrmu ;83
89 12687dd9 2023-08-04 jrmu ;(sum-of-ages Eva)
90 12687dd9 2023-08-04 jrmu ;210
91 12687dd9 2023-08-04 jrmu
92 12687dd9 2023-08-04 jrmu (define CURRENTYEAR 2009)
93 12687dd9 2023-08-04 jrmu
94 12687dd9 2023-08-04 jrmu (define (sum-of-ages a-ftree)
95 12687dd9 2023-08-04 jrmu (cond
96 12687dd9 2023-08-04 jrmu [(empty? a-ftree) 0]
97 12687dd9 2023-08-04 jrmu [else
98 12687dd9 2023-08-04 jrmu (+ (- CURRENTYEAR (child-date a-ftree))
99 12687dd9 2023-08-04 jrmu (sum-of-ages (child-father a-ftree))
100 12687dd9 2023-08-04 jrmu (sum-of-ages (child-mother a-ftree)))]))
101 12687dd9 2023-08-04 jrmu
102 12687dd9 2023-08-04 jrmu ;A list-of-eye-colors is either
103 12687dd9 2023-08-04 jrmu ;1. an empty list or
104 12687dd9 2023-08-04 jrmu ;2. (cons ec loec) where ec is a symbol and
105 12687dd9 2023-08-04 jrmu ;loec is a list-of-eye-colors.
106 12687dd9 2023-08-04 jrmu ;(alternatively written as (list ec1 ec2 ... empty)
107 12687dd9 2023-08-04 jrmu ;where each argument represents a symbol).
108 12687dd9 2023-08-04 jrmu ;
109 12687dd9 2023-08-04 jrmu ;eye-colors : ftn -> list-of-eye-colors
110 12687dd9 2023-08-04 jrmu
111 12687dd9 2023-08-04 jrmu (define (eye-colors a-ftree)
112 12687dd9 2023-08-04 jrmu (cond
113 12687dd9 2023-08-04 jrmu [(empty? a-ftree) empty]
114 12687dd9 2023-08-04 jrmu [else (append
115 12687dd9 2023-08-04 jrmu (list (child-eyes a-ftree))
116 12687dd9 2023-08-04 jrmu (eye-colors (child-father a-ftree))
117 12687dd9 2023-08-04 jrmu (eye-colors (child-mother a-ftree)))]))
118 12687dd9 2023-08-04 jrmu
119 12687dd9 2023-08-04 jrmu ;proper-blue-eyed-ancestor? : ftn -> boolean
120 12687dd9 2023-08-04 jrmu ;Given a-ftree, determine if the person
121 12687dd9 2023-08-04 jrmu ;has a proper blue eyed ancestor
122 12687dd9 2023-08-04 jrmu ;(someone in the family tree with blue eyes
123 12687dd9 2023-08-04 jrmu ; who is not the person in question)
124 12687dd9 2023-08-04 jrmu
125 12687dd9 2023-08-04 jrmu (define (proper-blue-eyed-ancestor? a-ftree)
126 12687dd9 2023-08-04 jrmu (cond
127 12687dd9 2023-08-04 jrmu [(empty? a-ftree) false]
128 12687dd9 2023-08-04 jrmu [else (or (blue-eyed-ancestor? (child-father a-ftree))
129 12687dd9 2023-08-04 jrmu (blue-eyed-ancestor? (child-mother a-ftree)))]))