;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #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"))))) ;;Model ;build-number : number number -> number ;Given new and previous, multiply previous by 10 and add new to build a new number for display in the calculator. (define (build-number new previous) (+ (* 10 previous) new)) ;A cell is either ;1. a number or ;2. a symbol. ;A gui-table is a (listof (listof cell)). (define pad1 '((1 2 3) (4 5 6) (7 8 9) ("#" 0 "*"))) (define pad2 '((1 2 3 "+") (4 5 6 "-") (7 8 9 "*") (0 "=" "." "/"))) ;;View ;pad->gui : string gui-table -> (listof (listof gui-items)) ;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. (define (pad->gui title a-gui-table) (append (list (list (make-message title)) (list (make-message ""))) (map loc->lob a-gui-table))) ;;Controller ;cell->button : cell -> gui-item [button] ;Given acell, create a gui-item [button]. ;gen-button : string -> gui-item [button] ;pad-callback : event -> true ;Draws acell (the text associated with the button) into title. (define (cell->button acell) (local ((define (gen-button cell-text) (make-button cell-text pad-callback)) (define (pad-callback event) (draw-text (convert->string acell)))) (gen-button (convert->string acell)))) ;draw-text : string -> true ;Given a-string, draw it into the title and return true. (define (draw-text a-string) (draw-message (list-ref (list-ref phonepad 1) 0) a-string)) ;convert->string : X -> string (define (convert->string a-datum) (cond [(number? a-datum) (number->string a-datum)] [else a-datum])) ;loc->lob : (listof cell) ;[(listof cells) to (listof buttons)] Given aloc, convert them to a lob. (define (loc->lob aloc) (map cell->button aloc)) (define phonepad (pad->gui "Phone" pad1)) (define calcpad (pad->gui "Calculator" pad2)) (create-window phonepad)