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-advanced-reader.ss" "lang")((modname |35.4|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;An address-book (ab) is either
5 12687dd9 2023-08-04 jrmu ;1. empty or
6 12687dd9 2023-08-04 jrmu ;2. (cons (cons s (cons n empty)) ab) [(list (list s n)
7 12687dd9 2023-08-04 jrmu ; ab)]
8 12687dd9 2023-08-04 jrmu ;where s is a symbol, n is a number, and ab is an address-book.
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu (define my-book empty)
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;add-to-address-book : symbol number -> void
13 12687dd9 2023-08-04 jrmu
14 12687dd9 2023-08-04 jrmu (define (add-to-address-book name phone)
15 12687dd9 2023-08-04 jrmu (set! my-book (cons (list name phone) my-book)))
16 12687dd9 2023-08-04 jrmu
17 12687dd9 2023-08-04 jrmu ;lookup : symbol ab -> number or false
18 12687dd9 2023-08-04 jrmu ;Lookup the phone number for aname in abook. Returns false if aname is not in abook.
19 12687dd9 2023-08-04 jrmu (define (lookup aname abook)
20 12687dd9 2023-08-04 jrmu (cond
21 12687dd9 2023-08-04 jrmu [(empty? abook) false]
22 12687dd9 2023-08-04 jrmu [(symbol=? (first (first abook)) aname) (second (first abook))]
23 12687dd9 2023-08-04 jrmu [else (lookup aname (rest abook))]))
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu ;remove : symbol -> void
26 12687dd9 2023-08-04 jrmu ;Removes the entry associated with aname from my-book.
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu (define (remove aname)
29 12687dd9 2023-08-04 jrmu (local ((define (remove-aux aname abook)
30 12687dd9 2023-08-04 jrmu (cond
31 12687dd9 2023-08-04 jrmu [(empty? abook) empty]
32 12687dd9 2023-08-04 jrmu [(symbol=? (first (first abook)) aname) (remove-aux aname (rest abook))]
33 12687dd9 2023-08-04 jrmu [else (cons (first abook)
34 12687dd9 2023-08-04 jrmu (remove-aux aname (rest abook)))])))
35 12687dd9 2023-08-04 jrmu (set! my-book (remove-aux aname my-book))))
36 12687dd9 2023-08-04 jrmu
37 12687dd9 2023-08-04 jrmu
38 12687dd9 2023-08-04 jrmu (begin (add-to-address-book 'Aaron 6265939257)
39 12687dd9 2023-08-04 jrmu (add-to-address-book 'Hsinya 9174633)
40 12687dd9 2023-08-04 jrmu (add-to-address-book 'Jessica 102853)
41 12687dd9 2023-08-04 jrmu (add-to-address-book 'Tuna 1234567)
42 12687dd9 2023-08-04 jrmu (remove 'Aaron))
43 12687dd9 2023-08-04 jrmu