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