On this page:
Temperature and Composition
Traffic Control
6.12

Lab 3: Conditional Eval

Implement this lab with the Beginning Student Language. Require the HtDP2e image and universe libraries at the top of your definitions:
(require 2htdp/image)
(require 2htdp/universe)

Choose the initial Head and Hands, and get started!

Temperature and Composition

Ex 1: Define a function what-temp that consumes a number (in degrees Fahrenheit) and produces one of three strings: "cold" for cold temperatures (say, less than 45), "hot" for hot temperatures (at or greater than 75), and "comfy" for anything in between.

Ex 2: Some international students (or just anyone tired of the Imperial system) may prefer to use Celsius for their input temperatures. Define a function celsius->fahrenheit that converts a number from degrees Celsius to degrees Fahrenheit. Search for the conversion online if you don’t know it off-hand.

Ex 3: Define a function what-temp/celsius that takes in a number (in degrees Celsius) and returns the same three strings as in Ex 1 for the appropriate temperature ranges.

Traffic Control

Swap Head and Hands.

You’ve been hired by UMD’s Department of Transportation Services to build their next generation of traffic lights, which work basically the same as all the existing traffic lights: they go from red to green to yellow. DOTS has specified that traffic lights should display red for 5 seconds, then green for 3 seconds, then yellow for 2 seconds (drivers better stay alert!).

Your job is to create a traffic light simulator using the animate function. Recall that animate uses a frame rate of 28 frames per second. The end result should look like this (visual embellishments are fine):

(Stop-light animation is missing)

Ex 4: Define a function which-light that, given a number between 0 and 280, returns an image of either the red, green, or yellow light. Think of the input to this function as the amount of time passed in a single traffic light cycle, measured in 1/28ths of a second. So if given 28, the function should respond with the appropriate light for being 1 second in to the cycle. If given 224, it should produce the light appropriate for 8 seconds in to the cycle.

Ex 5: Define a function time->light that, given any natural number representing 1/28ths of a second, returns the appropriate light for that moment in time. Animate your light by giving time->light to animate.

Hint: There may be a helpful, built-in numeric function to make time->light easy to implement with which-light.

Ex 6: Rework your program to use defined constants for any "magic numbers." It should be possible to change any of the following with a single edit to the program: the size of the light, the length of the light cycle, and the length of time for any of the light colors within the cycle.