On this page:
1 Conditionals
2 Data definitions
3 Enumerations
4 Examples of enumerations
5 Templates from enumerations
6 Functions on enumerations
7 Challenge:   intervals are cuts
8.12

Lab 3: Multiple cases🔗

Important Note Whenever you write a function in this class, you need to follow the design recipe.

1 Conditionals🔗

Conditionals are a feature of the Beginning Student Language and every other programming language. They choose which expression is evaluated based on which condition is true rather than false.

Let us say that we are cooking a turkey, and we want to measure the temperature of the thigh with a thermometer in Fahrenheit, and then use a function to tell us whether we should continue cooking or stop. We can design the function turkey as follows:
; turkey : Number –> String
; returns how to cook a turkey thigh at the given Fahrenheit temperature
; (define (turkey temperature) ...)
(check-expect (turkey 71) "Preheat the oven.")
(check-expect (turkey 72) "Preheat the oven.")
(check-expect (turkey 164) "Continue cooking.")
(check-expect (turkey 165) "Stop cooking.")
(check-expect (turkey 166) "Stop cooking.")
 
(define (turkey temperature)
  (cond [(<= temperature 72) "Preheat the oven."]
        [(< temperature 165) "Continue cooking."]
        [else "Stop cooking."]))

Exercise 1. Finish designing the following function.
; pie-dough : Number -> String
; returns what to do with pie dough at the given Fahrenheit temperature
; (define (pie-dough temperature) ...)
(check-expect (pie-dough 60) "wait")
(check-expect (pie-dough 70) "roll out")
(check-expect (pie-dough 80) "refrigerate")
Dough temp < 68°F = crumbly and dry. Dough temp > 75°F = sticky and wet.

Exercise 2. Design a function instructor? that checks whether a given string matches the name of one of your lab instructors. For example, if your lab instructors are Sita and Rama, then (instructor? "Sita") should be true and (instructor? "Ravana") should be false. You should find the function string=? in the Beginning Student Language useful:
; A Boolean is one of:
; - true
; - false
; Examples:
;   true
;   false
; Non-examples:
;   "true"
;   0
 
; string=? : String String -> Boolean
; checks whether the given strings are equal

Exercise 3. Design a function that takes a number and produces an image that is 956 wide and 400 tall. The image produced should be the yellow text "STAR" above "WARS", centered over a black background.
  • When the input is less than 100, the yellow text should become smaller as the input number becomes larger:
    • For example, when the input is 0, the yellow text in the output should have size 200.

    • To take another example, when the input is 99, the yellow text in the output should have size 2.

  • When the input is 100 or greater, the yellow text should disappear.

; star-wars : Number -> Image
; make the text STAR WARS shrink then disappear
; (define (star-wars t) ...)
(define background (rectangle 956 400 "solid" "black"))
(check-expect (star-wars 0)
              (overlay (above (text "STAR" 200 "yellow")
                              (text "WARS" 200 "yellow"))
                       background))
(check-expect (star-wars 99)
              (overlay (above (text "STAR" 2 "yellow")
                              (text "WARS" 2 "yellow"))
                       background))
(check-expect (star-wars 100) background)
(check-expect (star-wars 200) background)
Use the table method to finish the definition. What happens when you add the condition (< t 100) to the Beginning Student Tables tool? What happens when you (animate star-wars)?

2 Data definitions🔗

Follow the design recipe. Practice. Go to office hours even if you don't have a main question.

Every function you write in this class requires following the design recipe. According to the design recipe, we need to define our data (step 1) in order to write a function signature (step 2) and template (step 4).

Some data types are well-known and you don’t need to define them. For example, you don’t need to explicitly define Image, Number, String, or Boolean. You can also define your own data types using data definitions. The data definition can have any name that you want, but you should try to choose a meaningful name so that other people will understand it. And after your data definition, you can use your new data type in function signatures or even other data definitions.

Exercise 4. Chris is designing a function with the following signature.
; wibble : Wobble Cloud -> Image
In the very first step of the design recipe, which data definitions do they need to write? (You don’t need to write the data definitions. Just say which ones are needed, in a comment.)

3 Enumerations🔗

One kind of data definition is an enumeration. An enumeration lists each of the possible values. For example, consider the following Department enumeration.
; A Department is one of:
; - "biology"
; - "business"
; - "computer science"
; - "English"

Exercise 5. Put the data definition above into the Definitions Window of DrRacket. What happens when you run the program? What happens if you remove the semicolons and then run the program?

In this class, we put data definitions in comments because they are interpreted by other programmers, not by the computer. Some other programming languages (which you will learn in other classes) will read and enforce the data definitions, so they will not be in comments.

Exercise 6. A deck of cards has four suits: clubs, diamonds, hearts, and spades. Define an enumeration for a suit of cards.

Exercise 7. The colors of the rainbow are hot pink, red, orange, yellow, green, turquoise, indigo, and violet. Define an enumeration for the colors of the rainbow.

4 Examples of enumerations🔗

With the data definition of a Department in hand, we can define the following three examples of Departments:

(define alices-department "computer science")
(define bobs-department "business")
(define charlies-department "English")

Exercise 8. Define two examples of a suit of cards.

Exercise 9. Define two examples of a color of the rainbow.

Exercise 10. Below is an incorrect example of a Department. What happens when you run it?
(define dans-department "geology") ; Not an example of a Department

If you try to define an example which is not one of the possible values of the enumeration, you will not get an error message because the Beginning Student language does not enforce the data definition. But it will still be incorrect.

Exercise 11. Define a non-example of a color of the rainbow.

5 Templates from enumerations🔗

From the data definition, you can write a function template. A function template is written as a function definition with parts missing, which are replaced with ellipsis: ... (sometimes pronounced “dot dot dot”). The body of a function template typically includes crucial information which will guide the function definition.

Exercise 12. Is the following a function header or a function template? Justify your answer.
; draw-department : Department -> Image
; displays the department as image text
(define (draw-department d) ...)

Exercise 13. Examine the data definition of Department above. How many cases does it have? Then examine the template for processing a Department below. How many cases does it have? What do these numbers have in common? Why should they?
; process-department : Department -> ...
; ...
(define (process-department d)
  (cond [(string=? d "biology")          ...]
        [(string=? d "business")         ...]
        [(string=? d "computer science") ...]
        [(string=? d "English")          ...]))

The Beginning Student language can check the syntax of a template, but since a template is incomplete, the Beginning Student language cannot run it.

Exercise 14. Write a template for processing a suit of cards.

Exercise 15. Write a template for processing a color of the rainbow.

6 Functions on enumerations🔗

Finally, we can use the template to design a function by filling in the ellipses. Consider the following function which calculates the salary for each department:

; salary : Department -> Number
; returns the salary for each department.
 
(check-expect (salary "biology")          100000)
(check-expect (salary "business")         110000)
(check-expect (salary "computer science") 120000)
(check-expect (salary "English")          130000)
 
(define (salary d)
  (cond [(string=? d "biology")          100000]
        [(string=? d "business")         110000]
        [(string=? d "computer science") 120000]
        [(string=? d "English")          130000]))

Exercise 16. Examine the function designed above. How many examples (expressed as tests) does it have? How many template cases does it fill in? What do these numbers have in common? Why should they?

Exercise 17. Clubs are worth one point, diamonds are worth two points, hearts are worth three points, and spades are worth four points. Design a function suit-points which, given a suit of cards, will calculate how many points it is worth.

Exercise 18. Design a function next-color which, given a color of the rainbow, will return the next color. For example, red will return orange, and yellow will return green. Since violet is the last color, there is no color after it. So as a special case, violet should return hot pink, which is the first color.

7 Challenge: intervals are cuts🔗

In this section of the lab, you’ll make the beginning of a movie. That movie is Star Wars.

A movie is made of many shots. Our movie will be made of just 4 shots:
  • The blue text “A long time ago in a galaxy far, far away....” on a black background (150 frames)

  • Just a black background (50 frames)

  • The yellow text “STAR WARS” shrinking on a black background (100 frames)

  • The yellow text “It is a period of civil war. Rebel spaceships, striking”… rising on a black background (the rest of the movie)

So begin by defining the following constants. (Understand the code below.)
(require 2htdp/image)
(require 2htdp/universe)
 
(define background (rectangle 956 400 "solid" "black"))
(define epigraph1 "A long time ago in a galaxy far,")
(define epigraph2 "far away....")
(define story-text
  (above (text "It is a period of civil war." 40 "yellow")
         (text "Rebel spaceships, striking"   40 "yellow")
         (text "lorem ipsum dolor sit"        40 "yellow")))

Exercise 19. Define a constant image epigraph-text. It should be the blue text epigraph1 above epigraph2. A good size of the text might be 40.

Then define another constant image shot1. It should be the epigraph-text you just defined, centered over background.

Exercise 20. Design a function shot3 that produces the yellow text "STAR" above "WARS", centered over background. The function should take the text size (a number like 200 or 2) as input.

Exercise 21. Finish designing the following function:
; A Time is one of:
; - a number less than 150
; - a number at least 150 and less than 200
; - a number at least 200 and less than 300
; - a number at least 300
 
; star-wars-opening : Time -> Image
; returns the image at the given frame number
; in our Star Wars opening crawl
; (define (star-wars-opening t) ...)
(check-expect (star-wars-opening   0) shot1)
(check-expect (star-wars-opening 100) shot1)
(check-expect (star-wars-opening 149) shot1)
(check-expect (star-wars-opening 150) background)
(check-expect (star-wars-opening 199) background)
(check-expect (star-wars-opening 200) (shot3 200))
(check-expect (star-wars-opening 299) (shot3 2))
(check-expect (star-wars-opening 300)
              (place-image story-text (/ 956 2) 470 background))
(check-expect (star-wars-opening 350)
              (place-image story-text (/ 956 2) 420 background))
(check-expect (star-wars-opening 400)
              (place-image story-text (/ 956 2) 370 background))
Use the following template. Note how it corresponds to the data definition for Time.
(define (star-wars-opening t)
  (cond [(< t 150) (... t ...)]
        [(< t 200) (... t ...)]
        [(< t 300) (... t ...)]
        [else      (... t ...)]))
If you have trouble filling in a particular line of the template, apply the table method to the examples of that particular shot.

Exercise 22. Enjoy (animate star-wars-opening). Can you improve the movie?