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