Starting from:
$25

$19

The purpose of this lab is to practice defining and programming with conditional data.

Textbook references: Chapter 3: How To Design Programs Chapter 4: Intervals, Enumerations, and Itemizations
Note: Keep an eye on the Team’s general channel and make sure you rejoin when prompted. Your TAs will give you time estimates to regroup.
Shaping Up
Goals: To design programs over enumerations.
Starter Code: This is a data definition of a Shape.
; A Shape is one of:
; - "circle"
; - "square"
; - "triangle"
; and represents a kind of shape
Exercise (Reviewed) 1 Finish following the design recipe for this data definition (examples and a template).
Exercise (Reviewed) 2 Design a function draw which draws a Shape. Make use of your handy template from the past exercise, and don’t forget to follow every step of the design recipe for functions (signature, purpose statement, tests, code).
Exercise 3 Design a function draw/scene which overlays the image of a Shape on an empty-scene of a fixed size. Do you need to follow the Shape template here? How many tests does it need?
Exercise 4 Design a function next-shape which consumes a Shape and outputs the "next" Shape (any order is fine, so long as next-shape "cycles through" all the shapes.)
Exercise 5 Compose the draw/scene and next-shape functions in a big-bang animation that cycles through all of the shapes. Ask a member of the course staff for help if you’re stuck on big-bang syntax/usage. Using big-bang takes some practice, which is why we’re here! Note: Keep an eye on the clock and make sure to regroup in the general channel at the set time your TA has designated!
Exercise 6 Is your animation headache-inducingly fast? Slow it down by giving your on-tick clause a number that follows next-shape. The default is 1/28 seconds per tick; providing the on-tick handler with 1/2 would cause it to tick twice per second, whereas 2 would cause it to tick once every two seconds.
Exercise 7 Wrap your call to big-bang in a main function, which takes a Shape and uses that shape as the initial state. Don’t forget to give it a signature and purpose statement, but you cannot test the main program. Why not?
Exercise 8 Launch your animation by calling your main function from the interactions window.
Stop... when?
Goals: Introduces a big-bang handler that can stop the program.
Starter Code: Consider the following piece of code that uses big-bang to show an animation of a solar eclipse.
(require 2htdp/image)
(require 2htdp/universe)
 
(define SKY (rectangle 400 200 "solid" "midnightblue"))
(define SUN (circle 50 "solid" "yellow"))
(define MOON (circle 50 "solid" "lightgray"))
(define SUN-AND-SKY (overlay SUN SKY))
(define MOON-SPEED 5)
 
; draw-eclipse : Number -> Image
; Draws the sky, with the moon at a given position x
(define (draw-eclipse x)
  (place-image MOON x 100 SUN-AND-SKY))
 
; move-moon : Number -> Number
; Produces the next position of the moon given the current position
(define (move-moon x)
  (+ x MOON-SPEED))
 
; is-total-eclipse? : Number -> Boolean
; Is the moon fully eclipsing the sun?
(define (is-total-eclipse? x)
  (= x 200))
 
(big-bang 0
  [on-tick move-moon]
  [to-draw draw-eclipse]
  [stop-when is-total-eclipse?])
Exercise (Reviewed) 9
The above code introduces us to a new big-bang handler called stop-when. This allows the animation to stop once the moon overlaps the sun. stop-when takes in a moon’s x-coordinate and returns a boolean. If the result is true, the big-bang will stop and print the moon’s last position.
Go ahead and copy the starter code into DrRacket and run the animation. What condition is making the sun stop over the moon? How does it work?
Exercise (Reviewed) 10 Change the MOON-SPEED constant to 3.
(define MOON-SPEED 3)
What now happens when running the eclipse animation? What is the bug? How can you fix it?
Before You Go...
If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We love to teach and you will learn. It’s symbiotic!
Stretch Goals: Yellow Submarine
It is 1620 and Cornelius Drebbel has invented a new device. It’s like a boat but it goes underwater! He wants to show everyone his remarkable invention so they know how great it is. He is asking you to design a program that shows a ship sinking down under the waves. For the first 100 meters the ship will sink at a rate of 5 meters/second. For the next 200 meters the ship will sink at a rate of 4 meters/second. And then, for the next 100 meters it will sink very carefully at a rate of only 2 meters/second. After that Cornelius has decided the ocean is too dangerous to traverse.
Step 1: What stays the same?
Exercise 11 Define three constants, TOP-SPEED, MID-SPEED, and LOW-SPEED which define how many meters/second Cornelius’ ship can move at each depth.
Exercise 12 Define any other constants you might need for your program. Keep in mind that you will need to show the different ocean depths in different colors.
Step 2: What changes?
Exercise 13 Design a data definition for a SeaLevel. This data definition should encompass the three non-overlapping adjacent intervals mentioned above. We will use this data definition as the world state for our program. It will allow us to determine which speed the ship should be moving as it descends through the murky depths of the ocean. Remember to follow all the steps of the data design recipe.
Step 3: Which handlers do we need?
Exercise 14 Write down the signature and purpose statement for each handler function you will need. Every big-bang program needs a to-draw clause. What other clause will be useful to change the location of the ship as time passes? Which clause will help us stop the program when the ship reaches the bottom of the traversable ocean?
Step 4: Design your handlers
Exercise 15 Design the function that draws the ship at the correct location on the screen. Your program should show the entire traversable ocean, with the different depths as different colors since the ocean gets darker as you descend. Which handler does this belong to? Remember to follow the design recipe for functions.
Exercise 16 Design the function that moves the ship as time passes. The ship should descend at the rates specified by Cornelius. Which handler does this belong to? Remember to follow the design recipe for functions.
Exercise 17 Design the function which determines whether or not the program should stop. Your program should end when the ship reaches the bottom of the safely traversable ocean. Which handler does this belong to? Remember to follow the design recipe for functions.
Step 5: Put it all together!
Exercise 18 Write a function sinking-ship which, given the initial state of Cornelius’ submarine, uses big-bang to show the animation of the ship descending through the ocean. You do not need to follow the design recipe explicitly for this function (since you cannot test big-bang itself), and you cannot easily write its signature (since we don’t yet know what type of data big-bang returns). But you should still have sufficient tests for all the handler functions that big-bang uses!

More products