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-intermediate-reader.ss" "lang")((modname |23.1|) (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 #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 ;;Model
6 (define-struct pr (name number))
8 ;A phone-record (pr) is a structure
9 ;(make-pr name number)
10 ;where name is a symbol and number is a number
12 (define LOPR (list (make-pr 'John 6265939257)
13 (make-pr 'Aaron 6267217410)
14 (make-pr 'Hsinya 9195374958)))
16 ;search-name : symbol (listof pr)-> string
17 ;Given aname and alopr, find the phone-record containing aname in the name field and return its phone number as a string. Otherwise returns "name not found".
19 ;search-number : number (listof pr) -> string
21 (define (search-name aname alopr)
22 (cond
23 [(empty? alopr) "name not found"]
24 [(symbol=? aname (pr-name (first alopr))) (number->string (pr-number (first alopr)))]
25 [else (search-name aname (rest alopr))]))
27 (define (search-number anumber alopr)
28 (cond
29 [(empty? alopr) "number not found"]
30 [(= anumber (pr-number (first alopr))) (symbol->string (pr-name (first alopr)))]
31 [else (search-number anumber (rest alopr))]))
33 ;;View
35 (define search-text (make-text "Enter Name:"))
36 (define search-result (make-message "Find A Phone Number"))
38 ;;Controller
40 ;search-controller : event -> true
41 ;Extract the name or number from search-text and draw either the corresponding number or name in search-result.
43 ;search-controller : (X (listof pr) -> string) (string -> X) -> true
44 ;X is either a symbol or number
45 ;Extract the name or number from search-text and draw either the corresponding number or name in search-result.
47 (define (search-controller event)
48 (local ((define (search-controller search-op convert-op)
49 (draw-message search-result
50 (search-op
51 (convert-op (text-contents search-text)) LOPR)))
52 (define a-num? (string->number (text-contents search-text))))
53 (cond
54 [(number? a-num?) (search-controller search-number string->number)]
55 [else (search-controller search-name string->symbol)])))
57 (create-window (list (list search-text
58 (make-button "Search" search-controller))
59 (list search-result)))