-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path06-6-descendants.rkt
More file actions
executable file
·106 lines (83 loc) · 3.1 KB
/
06-6-descendants.rkt
File metadata and controls
executable file
·106 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
;; 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 06-2-descendants) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
;; descendants-better-tests.rkt
(require rackunit)
(require rackunit/text-ui)
(require "sets.rkt")
;; (require "extras.rkt") ; uncomment to get access to "provide"
(define-struct person (name children))
;; A Person is a
;; (make-person String ListOfPersons)
;; Interp: (make-person str lst) represents a person whose name is str
;; and whose children are represented by lst
;; A ListOfPersons (LoP) is one of
;; -- empty
;; -- (cons Person ListOfPersons)
(define alice (make-person "alice" empty))
(define bob (make-person "bob" empty))
(define chuck (make-person "chuck" (list alice bob)))
(define dave (make-person "dave" empty))
(define eddie (make-person "eddie" (list dave)))
(define fred (make-person "fred" (list chuck eddie)))
;;;; Template:
;;;; person-fn : Person -> ??
;;(define (person-fn p)
;; (... (person-name p) (lop-fn (person-children p))))
;;
;;;; lop-fn : ListOfPersons -> ??
;;(define (lop-fn lop)
;; (cond
;; [(empty? lop) ...]
;; [else (... (person-fn (first p))
;; (lop-fn (rest p)))]))
;; grandchildren : Person -> LoP
;; RETURNS: a list of the grandchildren of the given person.
;; (grandchildren fred) = (list alice bob dave)
;; STRATEGY: Use template for Person/LoP
(define (grandchildren p)
(all-children (person-children p)))
;; all-children : LoP -> LoP
;; GIVEN: a list of persons,
;; RETURNS: the list of all their children.
;; (all-children (list fred eddie)) = (list chuck eddie dave)
;; STRATEGY: Use template for Person/LoP
(define (all-children lop)
(cond
[(empty? lop) empty]
[else (append
(person-children (first lop))
(all-children (rest lop)))]))
(define-test-suite grandchildren-tests
(check set-equal?
(all-children (list fred eddie))
(list chuck eddie dave))
;; same test, but with the answer in a different order
(check set-equal?
(all-children (list fred eddie))
(list eddie chuck dave))
(check set-equal?
(grandchildren fred)
(list alice bob dave)))
;; descendants : Person -> LoP
;; GIVEN: a Person,
;; RETURNS: the list of his/her descendants
;; all-descendants : LoP -> LoP
;; GIVEN: a ListOfPersons,
;; RETURNS: the list of all their descendants
;; (descendants fred) = (list chuck eddie alice bob dave)
;; (all-descendants (list chuck eddie)) = (list alice bob eddie)
;; STRATEGY: Use template for Person/LoP
(define (descendants p)
(append
(person-children p)
(all-descendants (person-children p))))
(define (all-descendants lop)
(cond
[(empty? lop) empty]
[else (append
(descendants (first lop))
(all-descendants (rest lop)))]))
(begin-for-test
(check set-equal? (descendants fred) (list chuck eddie alice bob dave))
(check set-equal? (all-descendants (list chuck eddie)) (list alice bob dave)))