On this page:
1 Conditions:   cond
2 Star Wars

Lab 2: Conditionals

Important Note Whenever we say to "design a function", we mean that you need to follow the design recipe. Any other time that you write a function in this class, you also need to follow the design recipe.

1 Conditions: cond

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.

The following solution defines turkey-talk according to the design recipe.

; turkey-talk : Number –> String
; returns how to cook a turkey thigh at the given Fahrenheit temperature
; (define (turkey-talk temperature) ...)
(check-expect (turkey-talk 71) "Preheat the oven.")
(check-expect (turkey-talk 72) "Preheat the oven.")
(check-expect (turkey-talk 164) "Continue cooking.")
(check-expect (turkey-talk 165) "Stop cooking.")
(check-expect (turkey-talk 166) "Stop cooking.")
(define (turkey-talk temperature)
  (cond [(<= temperature 72) "Preheat the oven."]
        [(< temperature 165) "Continue cooking."]
        [else "Stop cooking."]))

Exercise 1 Finish designing the following function.
; fever : Number -> Boolean
; returns whether a given Fahrenheit thermometer reading indicates a fever
; (greater than 100 degrees)
; (define (fever t) ...)
(check-expect (fever 98) false)
(check-expect (fever 100) false)
(check-expect (fever 100.1) true)
(check-expect (fever 105) true)

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.

2 Star Wars

In the rest of this 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 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 3 Define a constant image background. It should be a black rectangle that is 956 pixels wide and 400 pixels tall.

Exercise 4 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 the background you just defined.

Exercise 5 Define yet another constant image shot3a. It should be the yellow text "STAR" above "WARS", centered over background. The text size should be 200. Be careful to make sure that the combined image size is same as background, not larger. You should find the function place-image in the 2htdp/image library useful.

Exercise 6 Define yet another constant image shot3b. It should be like shot3a, except the text size is reduced from 200 to 2. Again you should find the function place-image useful.

Exercise 7 Finish designing the following function:
; star-wars : Number -> Image
; returns the image at the given frame number
; in our Star Wars opening crawl
; (define (star-wars t) ...)
(check-expect (star-wars   0) shot1)
(check-expect (star-wars 100) shot1)
(check-expect (star-wars 149) shot1)
(check-expect (star-wars 150) background)
(check-expect (star-wars 199) background)
(check-expect (star-wars 200) shot3a)
(check-expect (star-wars 299) shot3b)
(check-expect (star-wars 300) (place-image story-text (/ 956 2) 470 background))
(check-expect (star-wars 350) (place-image story-text (/ 956 2) 420 background))
(check-expect (star-wars 400) (place-image story-text (/ 956 2) 370 background))
Use the following template:
(define (star-wars t)
  (cond [(< t 150) ...]
        [(< t 200) ...]
        [(< t 300) ...]
        [else ...]))
If you have trouble filling in a particular line of the template, apply the table method to the examples of that particular shot.

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