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-2) (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))
7 ;(make-star name instrument)
8 ;where both name and instrument are symbols.
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
20 ;where s is a star structure and los is a list-of-stars.
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)
27 [(empty? a-los) false]
28 [else (local ((define r (last-occurrence a-name (rest a-los))))
31 [(symbol=? a-name (star-name (first a-los))) (first a-los)]
34 (last-occurrence 'Matt
35 (list (make-star 'Matt 'violin)
36 (make-star 'Matt 'radio)))
40 [else (local ((define r (last-occurrence 'Matt (list (make-star 'Matt 'radio)))))
43 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
46 (define r-1 (last-occurrence 'Matt (list (make-star 'Matt 'radio))))
52 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
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)))
62 [else (local ((define r-2 (last-occurrence 'Matt empty)))
65 [(symbol=? 'Matt (star-name (make-star 'Matt 'radio))) (make-star 'Matt 'radio)]
68 (define r-2 (last-occurrence 'Matt empty))
74 [(symbol=? 'Matt (star-name (make-star 'Matt 'radio))) (make-star 'Matt 'radio)]
77 Now, we know that r-2 is
78 (last-occurrence 'Matt empty)
79 which is false, so r-2 is
86 [(symbol=? 'Matt 'Matt) (make-star 'Matt 'radio)]
93 [true (make-star 'Matt 'radio)]
97 (make-star 'Matt 'radio)
100 (define r-1 (make-star 'Matt 'radio))
105 [true (make-star 'Matt 'radio)]
106 [(symbol=? 'Matt (star-name (make-star 'Matt 'violin))) (make-star 'Matt 'violin)]
109 (make-star 'Matt 'radio)