CS 1410-20 Homework 3

Due: Friday, September 17th, 2010 9:40am

If we want to generalize our editor so that it can handle a mixture of text and pictures (like DrRacket’s editor), then we cannot use a string to represent the content of the editor. Instead, we can use a list—initially a list of strings, but we will eventually add other kinds of values to the list.

For this homework, we will use the following data definition:

  ;; A list-of-string is either
  ;;   - empty
  ;;   - (cons string list-of-string)

Part 1 – Content-Length

Write the function content-length, which takes a list of strings and returns the total number of characters in all strings combined.

For example, (content-length (cons "ee" (cons "eye" (cons "oh" empty)))) should produce 7.

Part 2 – Simple-Form?

Write the function simple-form?, which takes a list of strings and returns true if the lists contains only 1-character strings, false otherwise.

For example, (simple-form? (cons "ee" (cons "eye" (cons "oh" empty)))) should produce false, while (simple-form? (cons "a" empty)) should produce true.

Note that every string in the empty list contains only 1-character strings, since there are no strings in the list that have 0 or multiple characters.

Part 3 – Redact-Content

Write the function redact-content, which takes a list of strings and returns a list of the same length, but where every string in the list is replaced by "x".

For example, (redact-content (cons "ee" (cons "eye" (cons "oh" empty)))) should produce (cons "x" (cons "x" (cons "x" empty))).

Part 4 – Search-and-Delete

Write the function search-and-delete, which takes a string to search for and a list of strings. The search-and-delete function returns a list like the one that it is given, except that any instance of the string to search for is removed from the list.

For example, (search-and-delete "eye" (cons "ee" (cons "eye" (cons "oh" empty)))) should produce (cons "ee" (cons "oh" empty)). As another example, (search-and-delete "nee" (cons "ee" (cons "nee" (cons "mee" (cons "nee" empty))))) should produce (cons "ee" (cons "mee" empty)).

Part 5 – Insert

Write the function insert, which takes a string to insert, a position to insert the string, and a list of strings. The insert function returns a list like the one that it is given, except that the string to insert is added after the number of elements indicated by the position. If the position is beyond the end of the list (i.e., it is more than the number of strings in the list), then add the new string to the end of the list.

For example, (insert "nee" 5 empty) should produce (cons "nee" empty), and (insert "nee" 1 (cons "ee" (cons "eye" (cons "oh" empty)))) should produce (cons "ee" (cons "nee" (cons "eye" (cons "oh" empty)))).


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