On this page:
1 Introduction(s)
2 Purpose
3 A simple line text editor
4 Bonus
6.12

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):

image

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:

image

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:

image

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 image)""

(txt-string image)"Rip Torn"

(txt-string image)"Rip Torn"

(txt-string image)"Rip Torn"

Lab problem 2: txt-length

Design the function txt-length : Line -> Number.

(txt-length image)0

(txt-length image)8

(txt-length image)8

(txt-length image)8

Lab problem 3: txt-clear-left

Design the function txt-clear-left : Line -> Line.

(txt-clear-left image)image

(txt-clear-left image)image

(txt-clear-left image)image

(txt-clear-left image)image

Lab problem 4: txt-clear-right

Design the function txt-clear-right : Line -> Line.

(txt-clear-right image)image

(txt-clear-right image)image

(txt-clear-right image)image

(txt-clear-right image)image

Lab problem 5: txt-start

Design the function txt-start : Line -> Line.

(txt-start image)image

(txt-start image)image

(txt-start image)image

(txt-start image)image

Lab problem 6: txt-end

Design the function txt-end : Line -> Line.

(txt-end image)image

(txt-end image)image

(txt-end image)image

(txt-end image)image

Lab problem 7: txt-insert

Design the function txt-insert : Line String -> Line.

(txt-insert image "R")image

(txt-insert image "n")image

(txt-insert image "o")image

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 image)image

(txt-forward image)image

(txt-forward image)image

(txt-forward image)image

Lab problem 10: txt-backward

Design the function txt-backward : Line -> Line.

(txt-backward image)image

(txt-backward image)image

(txt-backward image)image

(txt-backward image)image

Lab problem 11: txt-backspace

Design the function txt-backspace : Line -> Line.

(txt-backspace image)image

(txt-backspace image)image

(txt-backspace image)image

(txt-backspace image)image

Lab problem 12: txt-transpose

Design the function txt-transpose : Line -> Line.

(txt-transpose image)image

(txt-transpose image)image

(txt-transpose image)image

(txt-transpose image)image

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.

So you will need to define two functions:
;; editor-key : Line KeyEvent -> Line
;; editor-draw : Line -> Image

The editor should respond to the following key events:

Lab problem 13: editor-key and editor-draw

Design the functions editor-key and editor-draw.