On this page:
1 Introduction(s)
2 Purpose
3 Deteriorating Texts
6.12

Lab 2: A World of Fun

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. Along the way, you’ll be exposed to some new operations for computing with strings and generating random numbers.

3 Deteriorating Texts

Your task is to design a program that displays a message that gradually deteriorates by losing letters until it has totally vanished. After all the letters have vanished, it should reappear and deteriorate again. An example is shown to the right, in which the phrase "veritas numquam perit" ("truth never perishes") deteriorates. (Note: the example is a looping GIF, so the phrase deteriorates in the same way every time; your program should have the phrase deteriorate randomly each time.)

You will need the 2htdp/image and 2htdp/universe libraries to complete this lab.

To help get you started, here is a function that renders a string as an image using a fixed-width font. It uses the 2htdp/image function text/font for selecting a font face. The details here are not important (although you can read the documentation if you’re interested). What is important is that if given two strings of the same length, text-tt will produce images of the same width.

Examples:
> (define (text-tt s)
    (text/font s 60 "black" "Courier" "default" "normal" "bold" #false))
> (text-tt "Hello!")

image

Here you can see that strings of the same length produce images of the same width:

Examples:
> (text-tt "George")

image

> (text-tt "Scooby")

image

To see why this is helpful, consider these examples.

Examples:
> (text-tt "Scooby")

image

> (text-tt "S ooby")

image

> (text-tt "S o by")

image

We can now set-up the use of big-bang to illustrate the basic idea of how this program will work. First we define a constant for the message we want to display when undeteriorated:

(define MSG "Truth never perishes")

The main function for this program will consume a string representing the message to be displayed. It uses text-tt to display the message. Here is a first attempt at the main function:

;; Show s as it deteriorates until gone and reappears as MSG
(define (main s)
  (big-bang s
    [to-draw text-tt]
    [on-tick identity]))

This use of big-bang represents the state of the world as a string: the message in its current state of deterioration. Initially it is just the complete message you want to display.

You can try this out by running:
(main MSG)

Notice that this works, except that nothing ever changes. That’s due to main’s use of the identity function. The identity function has a simple purpose: it simply gives back as a result any value it’s given as an argument.

Examples:
> (identity 7)

7

> (identity "Wilma")

"Wilma"

> (identity (text-tt "Yo"))

image

While identity may seem like a useless function, it serves a handy role here as a placeholder for an on-tick event handler function.

To complete this lab, you will to replace identity with a function of your own design that causes the current string to deteriorate one letter and restart once all letters are gone.

To accomplish this, we suggest writing three functions. The first takes a string and an index (a nonnegative integer between 0 and the length of the string) and replace whatever is at that index with a space. The second takes a string and replace a random spot in the string with a space (Hint: use the first function to help.) The third takes a string, determines if it consists only of whitespace (i.e. all spaces) and if so, produces MSG, otherwise it uses the second function to random replace a spot with a space.

Some functions worth knowing about to complete this lab: random, string-whitespace?, substring, string-append, string-length. Read the documentation for these functions, try out some examples, then try to solve the rest of this lab.

Lab problem 1: Replace with space at index

Define a function space-at that takes a string and index and replace whatever letter is at that index in the string with " ".

Examples:
> (space-at "Scooby" 0)

" cooby"

> (space-at "Scooby" 5)

"Scoob "

> (space-at "Scooby" 2)

"Sc oby"

Lab problem 2: Replace with space at random index

Define a function space-at-random that takes a string and replaces whatever a random place in the string with " ".

Examples:
> (space-at-random "Scooby")

"Sc oby"

> (space-at-random "Scooby")

"Sc oby"

> (space-at-random "Scooby")

" cooby"

> (space-at-random (space-at-random (space-at-random "Scooby")))

"Sc   y"

Lab problem 3: Start over if all whitespace

Define a function deteriorate that takes a string. If the string consists only of whitespace, then it produces MSG. Otherwise it replaces a random spot in the string with " ".

Examples:
> (deteriorate "Scooby")

"Sco by"

> (deteriorate "      ")

"Truth never perishes"

Once you have a working solution for these functions, you can modify your main function to replace identity with deteriorate and see what happens.

Lab problem 4: Try it out

Modify main to use deteriorate and run an example to see your code in action.