My solutions to the scheme text "How to Design Programs 2nd ed". (Or htdp2e) I aim to complete as many as these as I can - please feel free to comment on my solutions/answers!
Monday, 7 November 2011
Exercise 151 - Designing an editor
Exercise 151: Design the function create-editor.
The function consumes two strings and produces an Editor. The first string is the text to the left of the cursor and the second string is the text to the right of the cursor. The rest of the section relies on this function.
First up, I wanted to roll my own rev function instead of going off the one in the book. This was a fairly short question and so needed some padding to make it interesting!
; Lo1s -> Lo1s
; produce a reverse version of the given list
(check-expect
(rev (cons "a" (cons "b" (cons "c" empty))))
(cons "c" (cons "b" (cons "a" empty))))
(define (rev l)
(cond ((empty? l) l)
((cons? l) (add-to-end (first l)
(rev (rest l))))))
Now we need our wish-list function 'add-to-end'.
I didn't read this section of the manual as I wanted to come up with the solution to this on my own.
I had issues at this point as I kept wanting to use 'cons' to join the two lists together - this of course results in a nested list (which is not what we want). eg;
(cons (list "a" "b") (cons "a" empty))
=> (list (list "a" "b") "a")
After I discovered the 'append' function, this function shrank away to almost nothing. The only reason we need it now, is to save creating the second list with item and empty in the main function.
; adds an item to the end of the given list
(define (add-to-end item l)
(append l (cons item empty)
))
(check-expect (add-to-end "a" empty) (list "a"))
(check-expect (add-to-end "c" (list "a" "b"))
(list "a" "b" "c"))
Now, for the actual question - we need to make an Editor. This function consumes two strings and produces an Editor.
; An Editor is (make-editor Lo1S Lo1S)
; An Lo1S is one of:
; - empty
; - (cons 1String Lo1S)
(define-struct editor (pre post))
This method really seems overkill (or I'm not doing something I should be doing... it seems we've cut down on typing by 2 characters (the difference between m-a-k-e and c-r-e-a-t-e)
Perhaps my possible lack of comprehension will become clearer later on in exercise 152, but for now, this is what I think they are after ...
(define (create-editor left-string right-string)
(make-editor left-string right-string))
All these constants below are given by the question - I'm just pasting them here for completeness
; constants
(define HEIGHT 20) ; the height of the editor
(define WIDTH 200) ; its width
(define FTSZ 11) ; the font size
(define FTCL "black") ; the font color
; graphical constants - also supplied directly by the question
(define MT (empty-scene WIDTH HEIGHT))
(define CU (rectangle 1 HEIGHT "solid" "red"))
; Editor -> Image
; render an editor as an image of the two texts separated by the cursor
; (supplied directly from the question)
(define (editor-render e)
MT)
; Editor KeyEvent -> Editor
; deal with a key event, given some editor
; (supplied directly from the question)
(define (editor-kh ed ke)
ed)
; main : String -> Editor
; launch the editor given some initial string
; (supplied directly from the question)
(define (main s)
(big-bang (create-editor s "")
(on-key editor-kh)
(to-draw editor-render)))
At this point we can actually call our editor, but it doesn't do anything interesting yet. (Or anything at all really!)
(make-editor (make-editor "Hello" "World") "")
The first two test statements are supplied by the question. The rest are what we need to come up to answer the question.
; This first checks to make sure that when you press 'e' you
; create an editor displaying e
(check-expect (editor-kh (create-editor "" "") "e")
(create-editor "e" ""))
; This checks that when you type 'e', it's added at the
; cursor point
; (rather than the beginning or end of the editor)
(check-expect (editor-kh (create-editor "cd" "fgh") "e")
(create-editor "cde" "fgh"))
; check that pushing backspace deletes text from the
; correct location
(check-expect (editor-kh (create-editor "Bob" "Dylan") "\b")
(create-editor "Bo" "Dylan"))
(check-expect (editor-kh (create-editor "" "") "\b")
(create-editor "" ""))
; check we can use the cursor to move to the left
(check-expect (editor-kh (create-editor "Bob" "Dylan") "left")
(create-editor "Bo" "bDylan"))
(check-expect (editor-kh (create-editor "" "Dylan") "left")
(create-editor "" "Dylan"))
; check we can use the cursor to move to the right
(check-expect (editor-kh (create-editor "Bob" "Dylan") "right")
(create-editor "BobD" "ylan"))
(check-expect (editor-kh (create-editor "Bob" "") "right")
(create-editor "Bob" ""))
Subscribe to:
Posts (Atom)