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.
; 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."]))
; 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")
; 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
- 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.
So, one way to handle this case is to subtract twice the input from 200.
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)
2 Data definitions
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.
; wibble : Wobble Cloud -> Image
3 Enumerations
; 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.
(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.
; draw-department : Department -> Image ; displays the department as image text (define (draw-department d) ...)
; 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.
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)
(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.
; 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))
(define (star-wars-opening t) (cond [(< t 150) (... t ...)] [(< t 200) (... t ...)] [(< t 300) (... t ...)] [else (... t ...)]))
Exercise 22. Enjoy (animate star-wars-opening). Can you improve the movie?