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 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 12687dd9 2023-08-04 jrmu (define-struct star (name instrument))
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;A star is a structure
7 12687dd9 2023-08-04 jrmu ;(make-star name instrument)
8 12687dd9 2023-08-04 jrmu ;where both name and instrument are symbols.
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu (define alos
11 12687dd9 2023-08-04 jrmu (list (make-star 'Chris 'saxophone)
12 12687dd9 2023-08-04 jrmu (make-star 'Robby 'trumpet)
13 12687dd9 2023-08-04 jrmu (make-star 'Matt 'violin)
14 12687dd9 2023-08-04 jrmu (make-star 'Wen 'guitar)
15 12687dd9 2023-08-04 jrmu (make-star 'Matt 'radio)))
16 12687dd9 2023-08-04 jrmu
17 12687dd9 2023-08-04 jrmu ;A list-of-stars is either
18 12687dd9 2023-08-04 jrmu ;1. empty or
19 12687dd9 2023-08-04 jrmu ;2. (cons s los)
20 12687dd9 2023-08-04 jrmu ;where s is a star structure and los is a list-of-stars.
21 12687dd9 2023-08-04 jrmu ;
22 12687dd9 2023-08-04 jrmu ;last-occurrence : symbol list-of-star -> star/false
23 12687dd9 2023-08-04 jrmu ;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.
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu (define (last-occurrence a-name a-los)
26 12687dd9 2023-08-04 jrmu (cond
27 12687dd9 2023-08-04 jrmu [(empty? a-los) false]
28 12687dd9 2023-08-04 jrmu [else (local ((define r (last-occurrence a-name (rest a-los))))
29 12687dd9 2023-08-04 jrmu (cond
30 12687dd9 2023-08-04 jrmu [(star? r) r]
31 12687dd9 2023-08-04 jrmu [(symbol=? a-name (star-name (first a-los))) (first a-los)]
32 12687dd9 2023-08-04 jrmu [else false]))]))
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu (last-occurrence 'Matt
35 12687dd9 2023-08-04 jrmu (list (make-star 'Matt 'violin)
36 12687dd9 2023-08-04 jrmu (make-star 'Matt 'radio)))
37 12687dd9 2023-08-04 jrmu
38 12687dd9 2023-08-04 jrmu ;; maxi : non-empty-lon -> number
39 12687dd9 2023-08-04 jrmu ;; to determine the largest number on alon
40 12687dd9 2023-08-04 jrmu (define (maxi alon)
41 12687dd9 2023-08-04 jrmu (cond
42 12687dd9 2023-08-04 jrmu [(empty? (rest alon)) (first alon)]
43 12687dd9 2023-08-04 jrmu [else (cond
44 12687dd9 2023-08-04 jrmu [(> (first alon) (maxi (rest alon))) (first alon)]
45 12687dd9 2023-08-04 jrmu [else (maxi (rest alon))])]))
46 12687dd9 2023-08-04 jrmu
47 12687dd9 2023-08-04 jrmu (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)