CS 1410-20 Homework 1

Due: Friday, September 3rd, 2010 9:40am

Your job is to start the implementation of a text editor. For now, the editor will be very simple: it will always have a single line of characters (i.e., letters, numbers, spaces, and so on), and when the user types, the input is always added to the middle of the current text.

Part 1 – Middle

Implement the function middle, which takes a string and returns a number for half of the total number of characters in the string, rounding up in case of an odd number of characters.

For example, (middle "hello") should produce 3, while (middle "hola") should produce 2.

You will need the operations string-length and ceiling, among others.

For this part and all others, be sure to include contracts, purpose statements, and check-expect examples/tests in your program.

Part 2 – Insert At

Implement the function insert-at, which takes three arguments: a string orig, a number n, and a string new. The result is the same orig, except that new is inserted after the first n characters. Assume that n is between 0 and the length of orig, inclusive.

For example, (insert-at "hello" 3 "q") should produce "helqlo".

You will need the string-append operator, among others.

Part 3 – Insert At Middle

Implement the function insert-at-middle, which takes two arguments: a string orig and a string new. The result is a string that is like orig, but with new added in its middle.

For example, (insert-at-middle "hello" "q") should produce "helqlo".

Part 4 – First-Half Image

Implement the function first-half-image, which takes a string and returns an image for the first half of the string, where middle determines the boundary between the first and second halves of a string. The image should be in black, size-12 text.

For example, (first-half-image "hello") should produce an image that is “hel” in black text.

You will need the substring operator, among others. Don’t forget to include (require 2htdp/image) at the beginning of your program.

Part 5 – Second-Half Image

Implement the second-half-image function, which is like first-half-image, but for the art of the string after the middle.

Part 6 – Editor Image

Implement the editor-image function, which takes a string and produces an image that is the text of the string in size-12, black text, but with a 1-pixel wide by 14-pixel high rectangle drawn between the first half of the text and the second half of the text. The text image should be centered on a white box that is 14 pixels high and 300 pixels wide.

The skinny black rectangle corresponds to the insertion point of a text editor that always inserts characters into the middle of the text.

For example, (editor-image "hello") should produce an image of “hello”, but with a vertical line between the two “l”s.

 

You can now add (require 2htdp/universe) at the beginning of your program and then add

  (define (main starting-text)
    (big-bang starting-text
              (on-key insert-at-middle)
              (to-draw editor-image)))

In the interactions window (that is, not in the main program that you hand in, please), you can run (main "") to start your text editor.


Last update: Wednesday, October 20th, 2010
mflatt@cs.utah.edu