;; Use gui.ss teachpack ;;;;;;;;;;;;;;;;;;; The View ;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; digits-gui: N -> true ;; creates a GUI with menus allowing the user to select each of n digits ;; all this does is echo the number in a message field (define (digits-gui n) {local [(define digits (build-list 10 number->string)) (define digit-choosers (build-list n (lambda (i) (make-choice digits)))) (define msg-field (make-message " Select the digits ")) (define convert-button (make-button "Convert" (lambda (event) (draw-message msg-field (string-append "The number is " (number->string (convert (map choice-index digit-choosers))))))))] (create-window (list digit-choosers (list msg-field) (list convert-button)))}) ;;;;;;;;;;;;;;;;;; The Model ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; A Digit is a Number (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) ;; convert : (Listof Digit) -> Number ;; converts list of digits (ordered from most to least significant) to the corresponding number (define (convert alod) (convert-rev (reverse alod))) ;; convert-rev : (Listof Digit) -> Number ;; converts list of digits (ordered from least to most significant) to the corresponding number (define (convert-rev alod) (cond [(empty? alod) 0] [else (+ (first alod) (* 10 (convert-rev (rest alod))))])) ;; Examples/tests of convert-rev (= (convert-rev (list 3 2 1)) 123) (= (convert-rev (list 1 5 3 2)) 2351) ;; Examples/tests of convert (= (convert (list 3 2 1)) 321) (= (convert (list 9 8 7 6 5)) 98765) ;;;;;;;;;;;;;;;;;;; Run the program ;;;;;;;;;;;;;;;;;;;;;;; (digits-gui 4)