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-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 12687dd9 2023-08-04 jrmu ;;Model
5 12687dd9 2023-08-04 jrmu
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.
8 12687dd9 2023-08-04 jrmu
9 12687dd9 2023-08-04 jrmu (define (build-number new previous)
10 12687dd9 2023-08-04 jrmu (+ (* 10 previous) new))
11 12687dd9 2023-08-04 jrmu
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.
15 12687dd9 2023-08-04 jrmu
16 12687dd9 2023-08-04 jrmu ;A gui-table is a (listof (listof cell)).
17 12687dd9 2023-08-04 jrmu
18 12687dd9 2023-08-04 jrmu
19 12687dd9 2023-08-04 jrmu (define pad1 '((1 2 3)
20 12687dd9 2023-08-04 jrmu (4 5 6)
21 12687dd9 2023-08-04 jrmu (7 8 9)
22 12687dd9 2023-08-04 jrmu ("#" 0 "*")))
23 12687dd9 2023-08-04 jrmu (define pad2 '((1 2 3 "+")
24 12687dd9 2023-08-04 jrmu (4 5 6 "-")
25 12687dd9 2023-08-04 jrmu (7 8 9 "*")
26 12687dd9 2023-08-04 jrmu (0 "=" "." "/")))
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu ;;View
30 12687dd9 2023-08-04 jrmu
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.
33 12687dd9 2023-08-04 jrmu
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)))
38 12687dd9 2023-08-04 jrmu
39 12687dd9 2023-08-04 jrmu
40 12687dd9 2023-08-04 jrmu
41 12687dd9 2023-08-04 jrmu ;;Controller
42 12687dd9 2023-08-04 jrmu
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].
45 12687dd9 2023-08-04 jrmu
46 12687dd9 2023-08-04 jrmu ;gen-button : string -> gui-item [button]
47 12687dd9 2023-08-04 jrmu
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.
50 12687dd9 2023-08-04 jrmu
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))))
57 12687dd9 2023-08-04 jrmu
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.
60 12687dd9 2023-08-04 jrmu
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))
63 12687dd9 2023-08-04 jrmu
64 12687dd9 2023-08-04 jrmu ;convert->string : X -> string
65 12687dd9 2023-08-04 jrmu
66 12687dd9 2023-08-04 jrmu (define (convert->string a-datum)
67 12687dd9 2023-08-04 jrmu (cond
68 12687dd9 2023-08-04 jrmu [(number? a-datum) (number->string a-datum)]
69 12687dd9 2023-08-04 jrmu [else a-datum]))
70 12687dd9 2023-08-04 jrmu
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.
73 12687dd9 2023-08-04 jrmu
74 12687dd9 2023-08-04 jrmu (define (loc->lob aloc)
75 12687dd9 2023-08-04 jrmu (map cell->button aloc))
76 12687dd9 2023-08-04 jrmu
77 12687dd9 2023-08-04 jrmu (define phonepad (pad->gui "Phone" pad1))
78 12687dd9 2023-08-04 jrmu (define calcpad (pad->gui "Calculator" pad2))
79 12687dd9 2023-08-04 jrmu
80 12687dd9 2023-08-04 jrmu (create-window phonepad)