;; 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-beginner-reader.ss" "lang")((modname lab3-starter) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ; Design a world program word-processor that renders the characters that the user ; types in black text on a yellow background. (require 2htdp/image) (require 2htdp/universe) ; word-processor : String -> Boolean ; Runs a simplified word processor, returns #true if all went okay (define (word-processor initial-str) (big-bang initial-str [to-draw draw-wp] [on-key key-wp])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; to-draw event handler: (define BACKGROUND (empty-scene 600 100 "yellow")) (define WORD-SIZE 16) (define WORD-COLOR "black") ; draw-wp : String -> Image ; Draws the current text buffer (check-expect (draw-wp "hello") (overlay/align "left" "middle" (text "hello" WORD-SIZE WORD-COLOR) BACKGROUND)) (check-expect (draw-wp "a") (overlay/align "left" "middle" (text "a" WORD-SIZE WORD-COLOR) BACKGROUND)) (define (draw-wp s) (overlay/align "left" "middle" (text s WORD-SIZE WORD-COLOR) BACKGROUND)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; on-key event handler: ; key-wp : String KeyEvent -> String ; Adds key to the end of s, or removes the last letter if backspace is entered (check-expect (key-wp "e" "") "e") (check-expect (key-wp "l" "e") "le") (check-expect (key-wp "le" "\b") "l") (define (key-wp s key) (cond [(string=? key "\b") (remove-last s)] [else (string-append s key)])) ;; Helper function for key-wp: ; remove-last : String -> String ; Removes the last letter of the supplied string (check-expect (remove-last "") "") (check-expect (remove-last "apple") "appl") (define (remove-last s) (cond [(string=? s "") s] [else (substring s 0 (sub1 (string-length s)))]))