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-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 ;An address-book (ab) is either
5 ;1. empty or
6 ;2. (cons (cons s (cons n empty)) ab) [(list (list s n)
7 ; ab)]
8 ;where s is a symbol, n is a number, and ab is an address-book.
10 (define my-book empty)
12 ;add-to-address-book : symbol number -> void
14 (define (add-to-address-book name phone)
15 (set! my-book (cons (list name phone) my-book)))
17 ;lookup : symbol ab -> number or false
18 ;Lookup the phone number for aname in abook. Returns false if aname is not in abook.
19 (define (lookup aname abook)
20 (cond
21 [(empty? abook) false]
22 [(symbol=? (first (first abook)) aname) (second (first abook))]
23 [else (lookup aname (rest abook))]))
25 ;remove : symbol -> void
26 ;Removes the entry associated with aname from my-book.
28 (define (remove aname)
29 (local ((define (remove-aux aname abook)
30 (cond
31 [(empty? abook) empty]
32 [(symbol=? (first (first abook)) aname) (remove-aux aname (rest abook))]
33 [else (cons (first abook)
34 (remove-aux aname (rest abook)))])))
35 (set! my-book (remove-aux aname my-book))))
38 (begin (add-to-address-book 'Aaron 6265939257)
39 (add-to-address-book 'Hsinya 9174633)
40 (add-to-address-book 'Jessica 102853)
41 (add-to-address-book 'Tuna 1234567)
42 (remove 'Aaron))