On this page:
1 Derived works
2 Triangle stacks
3 Defining constants
4 Defining a function

Problem Set 1: Painting in DrRacket

Submit this assignment to ps1 on Handin. (If you are unable to connect to Handin, check to see if you have completed the first section of Lab 1. If you continue to have difficulties, contact the course staff.) Your submission is accepted only if the message "Handin successful" appears.

In this problem set, the data we will work with includes images. Add the following line to the Definitions Window, so that you will have access to the operations defined in the 2htdp/image library:

(require 2htdp/image)
1 Derived works

Problem 1: Write a program that paints a date when you hit the "Run" button, like this painting by On Kawara:

The text "OCT.31,1978" in white, centered on a black rectangular background

Use the functions text (which turns a string into an image) and overlay. For a background, two options are to copy-and-paste an image into DrRacket, or to use rectangle.

Make sure there is some margin space at the border of the date text.

Note There’s a maximum upload size for an acceptable submission to Handin: 7 MB. Be sure that any image that you’ve included in your file is small enough (you may need to rescale it in an image editor).

Problem 2: Are you familiar with the abstract painter Piet Mondrian? Create a digital painting similar to one of his famous works in which he uses few colors (at most black, white, red, blue and yellow) and rectilinear forms. Feel free to vary the palette and make your own abstract painting; or to faithfully reproduce one of his works. Use the functions rectangle and place-image.

2 Triangle stacks

Problem 3: Write a simple program which outputs a triangle, like this one:

Feel free to pick a different size and color.

Next, write a program which outputs three copies of that triangle stacked as follows:

Finally, write a program which outputs three copies of the triangle stack you just made stacked as follows:

3 Defining constants

What do you think about this way of writing programs? Imagine that your goal is to produce a stack of 81 triangles.... The program size is exploding!

In order to tame this explosion, we will need to learn a new feature of Beginning Student Language: being able to give names to expressions. Here is an example:

(define one-triangle (triangle 30 "solid" "red"))

Once you have this line in the Definitions Window, instead of writing

(triangle 30 "solid" "red")

you can just use the name that you have given to this expression: one-triangle. For example,

(beside one-triangle one-triangle)

means the same thing as

(beside (triangle 30 "solid" "red") (triangle 30 "solid" "red"))

Notice that when you type one-triangle in the Interactions Window (assuming that the definitions ran without errors), you should see a triangle.

We will also refer to giving a name to an expression as defining a constant.

Problem 4: First, define a constant named one-triangle which is the base triangle you used in the last section.

Second, define a constant named three-triangles which is the stack of three triangles from above. Use one-triangle.

Next, define a constant named nine-triangles which is the stack of nine triangles from above. Use three-triangles.

Finally, define a stack of 81 triangles named eighty-one-triangles:

(Hint: define one more constant which is a stack of triangles before defining the 81-triangle stack.)

4 Defining a function

You may notice at this point that although the explosion in program size has been tamed to some extent, we are still having to repeat a pattern manually each time we increase the triangle stack size. Each time we increase the triangle stack size, we take three copies of an image, place two copies side-by-side and the third copy on top. This is an operation. A template for this operation might look like this:

(above ... (beside ... ...))

where we further insist (somehow) that the "..."’s are all replaced with the same thing.

One way to make it clear that we want all of the "..."’s to be replaced with the same thing is to give them all the same name (for example, "t"):

(above t (beside t t))

So it seems that we would like the ability not only to give names to complete expressions, as in the last section, but to expressions with blanks in them, where the blanks themselves have names.

Here is an example of this feature being used in Beginning Student Language:
; stack : Image -> Image
; places two copies of the input image beside one another
; and a third copy on top
(define (stack t)
  (above t (beside t t)))

Add these five lines to the Definitions Window. Once you have these lines in the Definitions Window, you will have a new operation named stack at your disposal. For example,

(stack one-triangle)

will mean the same thing as

(above one-triangle (beside one-triangle one-triangle))

We will also refer to giving a name to an expression possibly with named blanks in it as defining a function or as defining an operation.

Exercise 5: Using only stack and one-triangle, write an expression which produces an 81-triangle stack, as above. (Hint: use stack more than once.)