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 |22.3|) (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 ;build-number : number number -> number
7 ;Given new and previous, multiply previous by 10 and add new to build a new number for display in the calculator.
9 (define (build-number new previous)
10 (+ (* 10 previous) new))
12 ;A cell is either
13 ;1. a number or
14 ;2. a symbol.
16 ;A gui-table is a (listof (listof cell)).
19 (define pad1 '((1 2 3)
20 (4 5 6)
21 (7 8 9)
22 ("#" 0 "*")))
23 (define pad2 '((1 2 3 "+")
24 (4 5 6 "-")
25 (7 8 9 "*")
26 (0 "=" "." "/")))
29 ;;View
31 ;pad->gui : string gui-table -> (listof (listof gui-items))
32 ;Given title and a-gui-table, return a (listof (listof gui-items)) that can be used by create-window to create a phone or calculator pad. pad->gui first creates a message gui-item with the string from title, then a message gui-item repeating the last item entered, and finally an array of button gui-items.
34 (define (pad->gui title a-gui-table)
35 (append (list (list (make-message title))
36 (list (make-message "")))
37 (map loc->lob a-gui-table)))
41 ;;Controller
43 ;cell->button : cell -> gui-item [button]
44 ;Given acell, create a gui-item [button].
46 ;gen-button : string -> gui-item [button]
48 ;pad-callback : event -> true
49 ;Draws acell (the text associated with the button) into title.
51 (define (cell->button acell)
52 (local ((define (gen-button cell-text)
53 (make-button cell-text pad-callback))
54 (define (pad-callback event)
55 (draw-text (convert->string acell))))
56 (gen-button (convert->string acell))))
58 ;draw-text : string -> true
59 ;Given a-string, draw it into the title and return true.
61 (define (draw-text a-string)
62 (draw-message (list-ref (list-ref phonepad 1) 0) a-string))
64 ;convert->string : X -> string
66 (define (convert->string a-datum)
67 (cond
68 [(number? a-datum) (number->string a-datum)]
69 [else a-datum]))
71 ;loc->lob : (listof cell)
72 ;[(listof cells) to (listof buttons)] Given aloc, convert them to a lob.
74 (define (loc->lob aloc)
75 (map cell->button aloc))
77 (define phonepad (pad->gui "Phone" pad1))
78 (define calcpad (pad->gui "Calculator" pad2))
80 (create-window phonepad)