;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname calc) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ()))) (require htdp/gui) (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 ;; sets WORKING (define (add-digit n) (begin (set! WORKING (+ n (* WORKING 10))) (draw-message total-message (number->string WORKING)))) ;; change-total : (num num -> num) num -> true ;; Sets TOTAL and WORKING (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 "/" /)))))) (make-calculator) (make-calculator)