On this page:
1 Designing functions
2 Composing functions
3 Extra fun
8.14

Lab 2: The design recipe🔗

Important Note: Whenever you write a function in this class, you need to follow the design recipe. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Color.

1) Prepare for the lab. Talk to your UI's. 2) Follow the design recipe. 3) Take a deep breath if your code fails. You got this!

1 Designing functions🔗

To practice the design recipe, let’s re-do some exercises from Problem set 2: Robert Indiana. Refer to your work on it, and feel free to ask questions about it as always.

Exercise 1. Design a function cube that takes a number as input and returns its cube. Write at least two examples using check-expect.

Exercise 2. Design a function diamond that takes a string and returns an image of it inside a circle inside a diamond. Write at least two examples using check-expect.

Hint: When the expected output of your example needs to be an image, make it in a separate DrRacket tab, so that the incomplete program in your main tab doesn’t interfere with getting the image right. Then, copy back to the main tab not the produced image but the expression you used to produce the image. Saving the expression in your example will help you come up with the definition of the function.

Exercise 3. Design a function arrow that takes two colors and returns an image of an arrow of the first given color inside a circle of the second given color. Write at least two examples using check-expect.

2 Composing functions🔗

Exercise 4. Design a function diptych that takes two strings and returns an image of them side-by-side, each inside a circle inside a diamond. Write at least two examples using check-expect.

Use your diamond function and the beside function provided by the 2htdp/image library; pay attention to their signatures:
; diamond : String -> Image
; beside : Image Image -> Image
Remember that using something means putting its name in your code, so your definition of diptych should contain the name diamond, and not the names overlay or text or circle.

Exercise 5. Design a function "womb" that takes 4 colors (not 8) and returns an image made of 4 arrows with the given colors, pointing in different directions. In particular, (womb "yellow" "blue" "red" "dark green") should produce an image like this:

Write at least two examples using check-expect. Use your arrow function, as well as the rotate, beside, and above functions provided by the 2htdp/image library; pay attention to their signatures:
; arrow : Color Color -> Image
; rotate : Number Image -> Image
; beside : Image Image -> Image
; above : Image Image -> Image
Again, using something means putting its name in your code, so your definition of womb should not contain overlay or triangle or rectangle or circle.

Exercise 6. Design a function cube-diamond that takes a number as input and returns an image of its cube inside a circle inside a diamond. Write at least two examples using check-expect.

Use your cube and diamond functions, as well as the built-in function number->string; pay attention to their signatures:
; cube : Number -> Number
; diamond : String -> Image
; number->string : Number -> String
Again, use cube and diamond and number->string, not * or expt or text.

3 Extra fun🔗

The flag of France Exercise 7. Design a function vertical-triband that takes three colors as inputs and produces a vertical triband flag image that is 150 wide and 100 tall, such as the flag of France. Write at least two examples using check-expect.

The flag of the Netherlands Exercise 8. Some triband flags have horizontal rather than vertical bands. For example, the flag of the Netherlands has three horizontal bands, whose colors are dark red, white, and dark blue. Use vertical-triband to design a function horizontal-triband that takes three colors as inputs and produces a horizontal triband flag image. Write at least two examples using check-expect.