On this page:
1 Functions on atomic data
2 Multiple cases
3 Structures
4 Nested templates
5 Unions

Lab 7: Midterm review

You may submit this lab to lab7 on Handin (this is optional).

Important: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.

1 Functions on atomic data

Exercise 1. Define a function tripled that consumes an image and produces a new image which contains exactly 3 copies of the original image side-by-side. For example, the expression (tripled ) should produce the image . Use the function beside provided by the 2htdp/image library.

Exercise 2. Define a function average that takes three numbers as input and returns their average.

2 Multiple cases

Exercise 3. Here is the data definition for a Flavor:
; A Flavor is one of:
; - "chocolate"
; - "strawberry"
; - "taro"

Write the template for Flavor. Do not write anything other than the template.

Exercise 4. Finish designing the following function.
; dessert : Flavor -> String
; return the best dessert with the given flavor
(define (dessert f) ...)
(check-expect (dessert "chocolate") "ice cream")
(check-expect (dessert "strawberry") "tart")
(check-expect (dessert "taro") "bubble tea")

Exercise 5. Design a function rate-flavor that takes a Flavor as input and returns a number between 0 and 10 to indicate how delicious the given flavor is. Use your own judgment as to how delicious each flavor is.

3 Structures

Exercise 6. Develop a data and structure definition for a Triple, which holds three numbers.

Exercise 7. Write down the names and signatures of all five courtesy functions created by your structure definition. Remember that the courtesy functions are the constructor, selector (or accessor) and predicate functions.

Exercise 8. Write the template for Triple.

Exercise 9. Design a function sorted-triple? which takes a Triple and produces a boolean indicating if the three numbers are in ascending order. For example, written in math notation, the triple (2, 2, 5) is sorted, but the triple (5, 2, 2) is not.

Exercise 10. Design a function average-triple which takes a Triple and produces the average of its three numbers.

Exercise 11. Design a function normalize-triple which takes a Triple and subtracts the average of its three numbers from each of its three numbers, producing a new Triple. For example, written in math notation, the triple (5, 2, 2) normalized is the triple (2, -1, -1).

4 Nested templates

Exercise 12. Develop a data and structure definition for a Flight, which holds three Flavors.

Exercise 13. Write the template for Flight. It should refer to the template for Flavor.

Exercise 14. Design a function rate-flight that takes a Flight as input and returns a number between 0 and 10 that is the average of the ratings produced by your rate-flavor function for the three Flavors in the given Flight.

5 Unions

Exercise 15. Write a data definition for MaybeNumber. A MaybeNumber is either the string "NaN" (which stands for “Not a Number”) or a Number.

Exercise 16. Write a function safe-div that takes two Numbers and tries to divide the first by the second. In the case that the second Number is 0, safe-div returns "NaN". Compare the behavior of safe-div and /.

Exercise 17. Here is a data definition of a union of structures:

; a Color is one of:
- (make-red   Number)
- (make-green Number)
- (make-blue  Number)
(define-struct red   [opacity])
(define-struct green [opacity])
(define-struct blue  [opacity])

Write the template for Color.

Exercise 18. Design a function change-opacity which takes a Number and a Color and returns a Color which is the same except that its opacity is the input number.

Exercise 19. Design a function which renders a given Color as an image somehow. The function’s output should vary with respect to the color and opacity of the input.