Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi, please help me to fix some errors in the Basic Student Language (BSL) DrRacket this program is simply the design of a simple one-line

hi, please help me to fix some errors in the Basic Student Language (BSL) DrRacket

this program is simply the design of a simple one-line editor

The first error was "string-empty?: this function is not defined" I think there is no mistake but it was actually an error

The second error was "substring: expects an exact-nonnegative-integer, given -1" as I know is it possible for substring input to be negative but I don't know why it was an error, I was also wondering if there is an alternative solution for this issue

note : I think there might be another error in this code :)

this is the code

Thank You.

(require spd/tags) (require 2htdp/image) (require 2htdp/universe)

(@problem 1)

;; A *SIMPLE* one line text editor ;; ;; The screen looks like: ;; ;; ab|c ;; ;; where | is the cursor. ;; ;; Typing characters inserts them. ;; left and right arrow moves cursor ;; delete removes character before cursor

(@htdw Editor) ;; ================= ;; Constants:

(define WIDTH 200) (define HEIGHT 20)

(define TEXT-SIZE 18) (define TEXT-COLOR "BLACK")

(define CURSOR (rectangle 1 20 "solid" "red"))

(define MTS (empty-scene WIDTH HEIGHT))

;; ================= ;; Data Definitions:

(@htdd Editor) (define-struct editor (pre post)) ;; Editor is (make-editor String String) ;; interp. (make-editor pre post) is an editor ;; pre is text before cursor ;; post is text after cursor

(define ED1 (make-editor "" "")) (define ED2 (make-editor "abc" "d")) (define ED3 (make-editor "abcd" "")) (define ED4 (make-editor "" "abcd"))

(@dd-template-rules compound) ;; 2 fields

(define (fn-for-editor e) (... (editor-pre e) (editor-post e)))

;; ================= ;; Functions:

(@htdf run-editor) (@signature String -> Editor) ;; run an editor, with pre as the initial text preceding the cursor

(@template-origin htdw-main) (define (run-editor pre) (big-bang (make-editor pre "") (to-draw render) ; Editor -> Image (on-key handle-key))) ; Editor KeyEvent -> Editor

(@htdf render) (@signature Editor -> Image) ;; place text with cursor at left, middle edge of MTS (check-expect (render (make-editor "a" "bc")) (overlay/align "left" "middle" (beside (text "a" TEXT-SIZE TEXT-COLOR) CURSOR (text "bc" TEXT-SIZE TEXT-COLOR)) MTS))

;(define (render e) MTS) ;stub

(@template-origin Editor)

(@template (define (render e) (... (editor-pre e) (editor-post e))))

(define (render e) (overlay/align "left" "middle" (beside (text (editor-pre e) TEXT-SIZE TEXT-COLOR) CURSOR (text (editor-post e) TEXT-SIZE TEXT-COLOR)) MTS))

(@htdf handle-key) (@signature Editor KeyEvent -> Editor) ;; call appropriate function for each keyboard command (check-expect (handle-key (make-editor "a" "b") "left") (cursor-left (make-editor "a" "b"))) (check-expect (handle-key (make-editor "a" "b") "right") (cursor-right (make-editor "a" "b"))) (check-expect (handle-key (make-editor "a" "b") "\b") (backspace (make-editor "a" "b"))) (check-expect (handle-key (make-editor "a" "b") "x") (insert (make-editor "a" "b") "x")) (check-expect (handle-key (make-editor "ab" "") "shift") (make-editor "ab" ""))

;(define (handle-key e key) e)

(@template-origin KeyEvent)

(@template (define (handle-key e ke) (cond [(key=? ke " ") (... (editor-pre e) (editor-post e))] [else e])))

(define (handle-key e key) (cond [(key=? key "left") (cursor-left e)] [(key=? key "right") (cursor-right e)] [(key=? key "\b") (backspace e)] [(= (string-length key) 1) (insert e key)] ;note, at this point key ; ;is a string 1 long, or ; ;what we call 1String [else e]))

(@htdf cursor-left) (@signature Editor -> Editor) ;; moves the cursor left by 1 (check-expect (cursor-left (make-editor "" "")) (make-editor "" "")) (check-expect (cursor-left (make-editor "" "ab")) (make-editor "" "ab")) (check-expect (cursor-left (make-editor "ab" "")) (make-editor "a" "b"))

;(define (cursor-left e) e)

(@template-origin Editor)

(@template (define (cursor-left e) (... (editor-pre e) (editor-post e))))

(define (cursor-left e) (if (string=? "" (editor-pre e)) e (make-editor (substring (editor-pre e) 0 -1) (string-append (string-ref (editor-pre e) -1) (editor-post e)))))

(@problem 2) (@htdf cursor-right) (@signature Editor -> Editor) ;; move the cursor right by 1 ;; !!! (check-expect (cursor-right (make-editor "" "")) (make-editor "" "")) (check-expect (cursor-right (make-editor "" "ab")) (make-editor "a" "b")) (check-expect (cursor-right (make-editor "ab" "")) (make-editor "" "ab"))

;(define (cursor-right e) e) ;stub

(@template (define (cursor-right e) (... (editor-pre e) (editor-post e))))

(define (cursor-right e) (if (string=? "" (editor-post e)) e (make-editor (string-append (editor-pre e) (string-ref (editor-post e) 0)) (substring (editor-post e) 1))))

(@problem 3) (@htdf backspace) (@signature Editor -> Editor) ;; delete character before cursor ;; !!! ;(define (backspace e) e) ;stub

(check-expect (backspace (make-editor "" "")) (make-editor "" "")) (check-expect (backspace (make-editor "" "ab")) (make-editor "a" "" )) (check-expect (backspace (make-editor "a" "")) (make-editor "" ""))

;(define (backspace e) e) ;stub

(@template (define (cursor-right e) (... (editor-pre e) (editor-post e))))

(define (backspace e) (if (string=? "" (editor-pre e)) e (make-editor (substring (editor-pre e) 0 (- (string-length (editor-pre e)) 1)) (editor-post e))))

(@problem 4) (@htdf insert) (@signature Editor 1String -> Editor) ;; insert s into e at cursor ;(define (insert e s) e) ;stub

(check-expect (insert (make-editor "" "")) (make-editor "" "a")) (check-expect (insert (make-editor "a" "a")) (make-editor "ab" "a")) (check-expect (insert (make-editor "a" "")) (make-editor "ab" ""))

;(define (insert e) e) ;stub

(@template (define (insert e) (... (editor-pre e) (editor-post e))))

(define (insert e s) (make-editor (string-append (editor-pre e) s) (editor-post e)))

(@htdf string-butlast) (@signature String -> String) ;; produce all but the last character of a string (check-expect (string-butlast "abcd") "abc") (check-expect (string-butlast "xyz") "xy")

;(define (string-butlast str) "")

(@template-origin String)

(@template (define (string-butlast str) (... str)))

(define (string-butlast str) (substring str 0 (sub1 (string-length str))))

(@htdf string-last) (@signature String -> String) ;; produce the last character of a string (check-expect (string-last "abcd") "d") (check-expect (string-last "def") "f")

;(define (string-last str) "")

(@template-origin String)

(@template (define (string-last str) (... str)))

(define (string-last str) (substring str (sub1 (string-length str))))

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions