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-beginner-reader.ss" "lang")((modname 12.2.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 mail structure is
5 12687dd9 2023-08-04 jrmu ;(make-mail f d m) where f and m are strings,
6 12687dd9 2023-08-04 jrmu ;d is a date.
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu (define-struct mail (from date message))
9 12687dd9 2023-08-04 jrmu ;
10 12687dd9 2023-08-04 jrmu ;A list-of-mail is either
11 12687dd9 2023-08-04 jrmu ;1. an empty list or
12 12687dd9 2023-08-04 jrmu ;2. (cons mail lom) where mail is a mail structure
13 12687dd9 2023-08-04 jrmu ;and lom is a list-of-mail.
14 12687dd9 2023-08-04 jrmu ;
15 12687dd9 2023-08-04 jrmu ;sort-mail : list-of-mail -> list-of-mail
16 12687dd9 2023-08-04 jrmu ;Given alom (a list-of-mail),
17 12687dd9 2023-08-04 jrmu ;sorts the mail based on name
18 12687dd9 2023-08-04 jrmu ;by insertion sorting and returns
19 12687dd9 2023-08-04 jrmu ;the sorted list.
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu (define (sort-mail alom)
22 12687dd9 2023-08-04 jrmu (cond
23 12687dd9 2023-08-04 jrmu [(empty? alom) empty]
24 12687dd9 2023-08-04 jrmu [(cons? alom) (insert (first alom) (sort-mail (rest alom)))]))
25 12687dd9 2023-08-04 jrmu
26 12687dd9 2023-08-04 jrmu ;insert : mail list-of-mail -> list-of-mail
27 12687dd9 2023-08-04 jrmu ;Given amail and alom, insert amail into the proper
28 12687dd9 2023-08-04 jrmu ;position in alom and return the new list-of-mail.
29 12687dd9 2023-08-04 jrmu ;This is done by comparing the name of amail with
30 12687dd9 2023-08-04 jrmu ;the name of the first mail in alom.
31 12687dd9 2023-08-04 jrmu ;
32 12687dd9 2023-08-04 jrmu ;Template
33 12687dd9 2023-08-04 jrmu ;(define (insert amail alom)
34 12687dd9 2023-08-04 jrmu ; (cond
35 12687dd9 2023-08-04 jrmu ; [(string<? (mail-from amail) (mail-from (first alom)))
36 12687dd9 2023-08-04 jrmu ; ...]
37 12687dd9 2023-08-04 jrmu ; [else ...]))
38 12687dd9 2023-08-04 jrmu
39 12687dd9 2023-08-04 jrmu (define (insert amail alom)
40 12687dd9 2023-08-04 jrmu (cond
41 12687dd9 2023-08-04 jrmu [(empty? alom) (cons amail alom)]
42 12687dd9 2023-08-04 jrmu [(string<? (mail-from amail) (mail-from (first alom)))
43 12687dd9 2023-08-04 jrmu (cons amail alom)]
44 12687dd9 2023-08-04 jrmu [else (cons (first alom)
45 12687dd9 2023-08-04 jrmu (insert amail (rest alom)))]))
46 12687dd9 2023-08-04 jrmu
47 12687dd9 2023-08-04 jrmu ;Test
48 12687dd9 2023-08-04 jrmu ;(define POSTMAN (cons (make-mail "Sally" 4561923 "Hi it's me Sally") (cons (make-mail "Jennifer" 9133217 "Sitting at home") (cons (make-mail "Tweety" 1234567 "Add me on Twitter") (cons (make-mail "Aaron" 5939257 "RonRon") (cons (make-mail "Zebrafish" 8888888 "Alphabetical") (cons (make-mail "Jabra" 2950 "Headset") empty)))))))
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu ;; search : number list-of-numbers -> boolean
51 12687dd9 2023-08-04 jrmu (define (search n alon)
52 12687dd9 2023-08-04 jrmu (cond
53 12687dd9 2023-08-04 jrmu [(empty? alon) false]
54 12687dd9 2023-08-04 jrmu [else (or (= (first alon) n) (search n (rest alon)))]))
55 12687dd9 2023-08-04 jrmu ;
56 12687dd9 2023-08-04 jrmu ;search-sorted : number list-of-numbers -> boolean
57 12687dd9 2023-08-04 jrmu ;Determines if n is in alon and returns true if it is,
58 12687dd9 2023-08-04 jrmu ;false otherwise. alon is assumed to be sorted
59 12687dd9 2023-08-04 jrmu ;in descending order. Performs a linear-search.
60 12687dd9 2023-08-04 jrmu ;
61 12687dd9 2023-08-04 jrmu ;Template
62 12687dd9 2023-08-04 jrmu ;(define (search-sorted n alon)
63 12687dd9 2023-08-04 jrmu ; (cond
64 12687dd9 2023-08-04 jrmu ; [(empty? alon) ...]
65 12687dd9 2023-08-04 jrmu ; [(= n (first alon)) ...]
66 12687dd9 2023-08-04 jrmu ; [else ... (search-sorted (rest alon))]))
67 12687dd9 2023-08-04 jrmu
68 12687dd9 2023-08-04 jrmu (define (search-sorted n alon)
69 12687dd9 2023-08-04 jrmu (cond
70 12687dd9 2023-08-04 jrmu [(empty? alon) false]
71 12687dd9 2023-08-04 jrmu [(= n (first alon)) true]
72 12687dd9 2023-08-04 jrmu [(> n (first alon)) false]
73 12687dd9 2023-08-04 jrmu [else (search-sorted n (rest alon))]))
74 12687dd9 2023-08-04 jrmu
75 12687dd9 2023-08-04 jrmu ;Test
76 12687dd9 2023-08-04 jrmu ;(define MRLIST (cons 15 (cons 12 (cons 12 (cons 9 (cons 3 (cons 0 empty)))))))