Lab 4: Typing, Cursing, Texting
1 Introduction(s)
You’ll work in labs in pairs. Find someone to work with for this first lab and introduce yourself.
Make sure at least one of you have a laptop to work on for this lab.
The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. We’ll have you switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.
2 Purpose
In this lab, you’ll practice writing functions and building simple world programs using the design recipe for systematic computational problem solving.
3 A simple line text editor
In this lab, you will write the core functions that are part of a very simple text editor for a single line of text (this could be part of a messaging app, a search bar, an SMS application, etc.). This program lets a user type in a line of text and move a cursor around and edit that text.
The heart of this program is the following data definition:
;; A Line is a (make-txt String String) ;; Interpretation: (make-txt string1 string2) is a line of text consisting ;; of string1 and string2 with a cursor placed between them. (define-struct txt (left right))
So for example if someone has typed something that looks like this (where the red bar is the cursor):
it would be represented with the data (make-txt "where u" " at?").
The user could move the cursor left causing the line to look like:
which would be represented as (make-txt "where " "u at?").
The user could use the backspace (or delete) key to erase the character to the left of the cursor:
which would be represented as (make-txt "where" "u at?").
You will not be making the text editor (yet), but rather will design a handful of functions that will be useful in building the editor. For each function, you will be given a signature and a couple of examples presented visually. It will be up to you to map this visual presentation of the information in to the data representation.
For each function, use the steps of the design recipe to arrive at a correct solution for the function.
You will need to copy the data definition from above in to the top of your file.
Lab problem 1: txt-string
Design the function txt-string : Line -> String.(txt-string ) ⟹ ""
(txt-string ) ⟹ "Rip Torn"
(txt-string ) ⟹ "Rip Torn"
(txt-string ) ⟹ "Rip Torn"
Lab problem 2: txt-length
Design the function txt-length : Line -> Number.(txt-length ) ⟹ 0
(txt-length ) ⟹ 8
(txt-length ) ⟹ 8
(txt-length ) ⟹ 8
Lab problem 3: txt-clear-left
Design the function txt-clear-left : Line -> Line.(txt-clear-left ) ⟹
(txt-clear-left ) ⟹
(txt-clear-left ) ⟹
(txt-clear-left ) ⟹
Lab problem 4: txt-clear-right
Design the function txt-clear-right : Line -> Line.(txt-clear-right ) ⟹
(txt-clear-right ) ⟹
(txt-clear-right ) ⟹
(txt-clear-right ) ⟹
Lab problem 5: txt-start
Design the function txt-start : Line -> Line.(txt-start ) ⟹
(txt-start ) ⟹
(txt-start ) ⟹
(txt-start ) ⟹
Lab problem 6: txt-end
Design the function txt-end : Line -> Line.(txt-end ) ⟹
(txt-end ) ⟹
(txt-end ) ⟹
(txt-end ) ⟹
Lab problem 7: txt-insert
Design the function txt-insert : Line String -> Line.(txt-insert "R") ⟹
(txt-insert "n") ⟹
(txt-insert "o") ⟹
For the next set of functions, a small set of functions may be useful. Finish their design based on their given signature and purpose statements. They each consume a non-empty string. (Do not worry about what happens if the string is empty.)
Lab problem 8: Helpers
;; String -> String ;; Produce the first character (define (string-first s) ...) ;; String -> String ;; Produce all but the first character (define (string-drop-first s) ...) ;; String -> String ;; Produce the last character (define (string-last s) ...) ;; String -> String ;; Produce all but the last character (define (string-drop-last s) ...)
And now for the rest of the line operations.
Lab problem 9: txt-forward
Design the function txt-forward : Line -> Line.(txt-forward ) ⟹
(txt-forward ) ⟹
(txt-forward ) ⟹
(txt-forward ) ⟹
Lab problem 10: txt-backward
Design the function txt-backward : Line -> Line.(txt-backward ) ⟹
(txt-backward ) ⟹
(txt-backward ) ⟹
(txt-backward ) ⟹
Lab problem 11: txt-backspace
Design the function txt-backspace : Line -> Line.(txt-backspace ) ⟹
(txt-backspace ) ⟹
(txt-backspace ) ⟹
(txt-backspace ) ⟹
Lab problem 12: txt-transpose
Design the function txt-transpose : Line -> Line.(txt-transpose ) ⟹
(txt-transpose ) ⟹
(txt-transpose ) ⟹
(txt-transpose ) ⟹
4 Bonus
If you’ve done the first part of the lab and would like to go further, you can try your hand at buiding the line editor. This is not required; it’s just here in case you’re looking for more to do.
The functions above implement much of the functionality needed in a text line editor. Now you’ll need to put the peices together, using the 2htdp/image and 2htdp/universe libraries to create the editor.
Here is a main function that consumes a string and launches a world program where the state of the world is represented as a Line:
;; String -> Line ;; Launch a line editor starting with given string and cursor on left (define (main s) (big-bang (make-txt "" s) [on-key editor-key] [to-draw editor-draw]))
The program should respond to keyboard events and will need a function for displaying the current line, with an indication of where the cursor is.
;; editor-key : Line KeyEvent -> Line ;; editor-draw : Line -> Image
The editor should respond to the following key events:
"\b": delete one character to the left of the cursor, if there is one,
"left": moves cursor left, if possible,
"right": moves cursor right, if possible,
"up": moves cursor to start of line,
"down": moves cursor to end of line,
"f1" (function key 1): transposes letter before and after cursor, if possible,
"f2" (function key 2): deletes text after cursor,
"f3" (function key 3): deletes text before cursor, and
any length 1 string (except "\b" and "\r") is inserted to the left of cursor.
Lab problem 13: editor-key and editor-draw
Design the functions editor-key and editor-draw.