On this page:
1 Conditions:   cond
2 "Inline" Conditions
3 Conditionals
4 Images and Conditionals

Lab 2: Conditionals

1 Conditions: cond

Conditionals are a feature of Racket and every other programming language. They control which expression is evaluated based on a condition which is either true or 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 Racket function to tell us whether we should continue cooking or stop.

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

; turkey-talk: Integer –> String
; (turkey-talk temperature) 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 Racket function to tell us whether we should continue cooking or stop.
(define (turkey-talk temperature)
  (cond [(<= temperature 72) "Preheat the oven."]
        [(< temperature 165) "Continue cooking."]
        [else "Stop cooking."]))
(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.")
2 "Inline" Conditions

We start with a rotating star function:

; tilted-star : Num -> Image
; (tilted-star deg) produces a gold star tilted by deg degrees
(define (tilted-star deg)
  (rotate deg (star 100 "solid" "goldenrod")))

Exercise 1: Adapt it so that the star alternates between gold and white, depending on the parity of the degrees parameter:

; flickering-tilted-star : Num -> Image
; (flickering-tilted-star deg) produces a gold star tilted by deg degrees
; if deg is even, and produces an invisible white star tilted by deg degrees
; if deg is odd (thus the star "flickers" when animated)
(define (flickering-tilted-star deg)
  (rotate deg (star 100 "solid" (cond [(even? deg) "goldenrod"] [else "white"]))))

Call (animate flickering-tilted-star), and the star will flicker quickly.

3 Conditionals

A decibel meter is like a thermometer for sound. Hold it near a noise, and it will tell you hold loud it is in decibels. For example, a jet engine is one hundred fifty decibels, and a leaf rustling is ten decibels.

Exercise 2: Your exercise is to define a function decibels that does the reverse. Given a number of decibels, return something that could cause that much noise. Ask the students to apply the design recipe to this exercise.

The following table shows various noises and their respective ranges of decibels:

Whispering                          30-39 dB

Television                          60-69 dB

Honda S2000 Factory Exhaust         98-99 dB

Ferrari 458 Factory Exhaust         101-105 dB

McDonnell Douglas F-15C at takeoff  145-149 dB

Lockheed Martin F-35B at hover      150-154 dB

Thermonuclear warhead               280-300 dB

Disaster Area, heard from a concrete bunker 37 mi away: > 1000 dB.

4 Images and Conditionals

Exercise 3: Design a function which accepts a string, which may be either "circle" "box", or "triangle", another string which indicates color, i.e. "red", blue", etc..., and a number representing the total width, and produces either a circle image, or a box image with the appropriate color.

An example of a functional example is
;; (draw-shape "circle" "red" 50) -> (circle 25 "solid" "red")

You should come up with at least two more functional examples, and implement them as test cases.

Exercise 4: Define the function red-frame, which consumes an image and draws a red frame around it. Note, most images are not rectangular, so you need to extend them with a white background so that they are rectanglular. For example, say you have a circle of width (and height) 100, this should be first overlayed on top of a white rectangle of size 101 by 101. In order to draw a red background, you may overlay this image over a rectanble of size 104 by 104.

Follow the design recipie, you should use the following unit test, and come up with two more of your own.

(check-expect (red-frame (circle 50 "solid" "blue"))
              (overlay (circle 50 "solid" "blue")
                       (rectangle 101 101 'solid 'white)
                       (rectangle 104 104 'solid 'red)))