Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-reader.ss" "lang")((modname 18.1.11) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
4 (define-struct star (name instrument))
6 ;A star is a structure
7 ;(make-star name instrument)
8 ;where both name and instrument are symbols.
10 (define alos
11 (list (make-star 'Chris 'saxophone)
12 (make-star 'Robby 'trumpet)
13 (make-star 'Matt 'violin)
14 (make-star 'Wen 'guitar)
15 (make-star 'Matt 'radio)))
17 ;A list-of-stars is either
18 ;1. empty or
19 ;2. (cons s los)
20 ;where s is a star structure and los is a list-of-stars.
21 ;
22 ;last-occurrence : symbol list-of-star -> star/false
23 ;Given a-name and a-los, find the last occurrence of a star with a-name in the name field and return this star structure. Return false if there is no star with a-name.
25 (define (last-occurrence a-name a-los)
26 (cond
27 [(empty? a-los) false]
28 [else (local ((define r (last-occurrence a-name (rest a-los))))
29 (cond
30 [(star? r) r]
31 [(symbol=? a-name (star-name (first a-los))) (first a-los)]
32 [else false]))]))
34 (last-occurrence 'Matt
35 (list (make-star 'Matt 'violin)
36 (make-star 'Matt 'radio)))
38 (cond
39 [false false]
40 [else (local ((define r (last-occurrence 'Matt (list (make-star 'Matt 'radio)))))
41 (cond
42 [(star? r) r]
43 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
44 [else false]))])
46 (define r-1 (last-occurrence 'Matt (list (make-star 'Matt 'radio))))
48 (cond
49 [false false]
50 [else (cond
51 [(star? r-1) r-1]
52 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
53 [else false])])
54 #|
55 Now, we know that r-1 is
56 (last-occurrence 'Matt (list (make-star 'Matt 'radio)))
58 (last-occurrence 'Matt (list (make-star 'Matt 'radio)))
60 (cond
61 [false false]
62 [else (local ((define r-2 (last-occurrence 'Matt empty)))
63 (cond
64 [(star? r-2) r-2]
65 [(symbol=? 'Matt (star-name (make-star 'Matt 'radio))) (make-star 'Matt 'radio)]
66 [else false]))])
68 (define r-2 (last-occurrence 'Matt empty))
70 (cond
71 [false false]
72 [else (cond
73 [(star? r-2) r-2]
74 [(symbol=? 'Matt (star-name (make-star 'Matt 'radio))) (make-star 'Matt 'radio)]
75 [else false])])
77 Now, we know that r-2 is
78 (last-occurrence 'Matt empty)
79 which is false, so r-2 is
80 false
82 (cond
83 [false false]
84 [else (cond
85 [false r-2]
86 [(symbol=? 'Matt 'Matt) (make-star 'Matt 'radio)]
87 [else false])])
89 (cond
90 [false false]
91 [else (cond
92 [false r-2]
93 [true (make-star 'Matt 'radio)]
94 [else false])])
96 Hence, r-1 is
97 (make-star 'Matt 'radio)
98 |#
100 (define r-1 (make-star 'Matt 'radio))
102 (cond
103 [false false]
104 [else (cond
105 [true (make-star 'Matt 'radio)]
106 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
107 [else false])])
109 (make-star 'Matt 'radio)