;; This code require the "new-gui.ss" teachpack, which supplies ;; create-window : list-of-list-of-control -> window ;; make-text : string -> control ;; text-contents ; control[text] -> string ;; make-button ; string (evt -> true) -> control ;; make-message ; string -> control ;; draw-message ; control[message] string -> true (define (make-calculator) (local [(define TOTAL 0) (define WORKING 0) (define PREV-OP +) (define total-message (make-message (number->string TOTAL))) ;; op-button : string (num num -> num) -> button (define (op-button label OP) (make-button label (lambda (evt) (begin (change-total PREV-OP WORKING) (set! PREV-OP OP) true)))) ;; digit-button : num -> button (define (digit-button n) (make-button (number->string n) (lambda (evt) (add-digit n)))) ;; add-digit : num -> true (define (add-digit n) (begin (set! WORKING (+ n (* WORKING 10))) (draw-message total-message (number->string WORKING)))) ;; change-total : (num num -> num) num -> true (define (change-total OP amt) (local [(define new-total (OP TOTAL amt))] (begin (set! TOTAL new-total) (set! WORKING 0) (draw-message total-message (number->string new-total)))))] (create-window (list (list total-message) (map digit-button '(7 8 9)) (map digit-button '(4 5 6)) (map digit-button '(1 2 3)) (map digit-button '(0)) (list (op-button "+" +) (op-button "-" -) (op-button "*" *) (op-button "/" /) (op-button "=" (lambda (tot new) new))))))) (make-calculator) (make-calculator)