On this page:
Understanding Booleans
Simplification with Booleans
The Design Recipe
Designing World Programs
Before You Go...
7.9

Lab 2 Booleans, the Design Recipe, and World Programs

lab!

Purpose: The purpose of this lab is to review the Boolean data type, and to practice the Design Recipe, and World Program design.

Understanding Booleans

There are three main Boolean operators: and, or, and not. Do you understand how they work in logic? They work the same in BSL. Let’s see:

Exercise 1 Imagine or was not built in to BSL. Design a function or, following all steps of the (function) design recipe!

Exercise 2 What changes if, instead of or, you had to define and? what about not?

Simplification with Booleans

In general we want our code to be as simple as possible in order to make it easy for other people to read it. The concept of simplifying code comes up a lot when working with Booleans.

Exercise 3 Simplify each of the following expressions as much as possible using the Boolean operators and, or, and not. In each case, first think about what you know about the types of data of x and y. Are they necessarily Booleans? something else?

The Design Recipe

The design recipe will be our go-to strategy for tackling any problem from here on.

Recall from lecture that there are four steps of the data design recipe: definition, interpretation, examples, and template.

Likewise, there are four steps of the function design recipe: signature, purpose statement, tests, and code (which has to follow the template).

In this course, when we ask you to design data, you must follow the data design recipe. When we ask you to design functions, you must follow the function design recipe. Always include all relevant steps unless the problem states otherwise.

Note: You will never be penalized for following the design recipe when you aren’t required to. If a problem states "Write the code for a function that...", this is only asking for body of the function. However, including a signature, purpose statement, and template may still be a good idea.

Exercise 4 The city council is updating the program that controls the traffic lights. They have asked for your help designing data to represent a traffic light. A traffic light can be red, yellow, or green.

Exercise 5 Using your traffic light data definition from above, design the function next-light which produces the next color in the sequence. Recall that a traffic light goes from green to yellow, yellow to red, and from red back to green again.

Exercise 6 It is 1620 and Cornelis Drebbel has invented a new device. It’s like a boat but it goes underwater! 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 Cornelis has decided it would be too dangerous to dive deeper. Design data to represent a depth, accounting for the fact that the ship will have different behavior at different depths.

Exercise 7 Using your depth data definition from above, design the function move-ship which takes in the current depth of Cornelis’ ship and produces its new depth after 1 second of travel. Be sure to refer to the description above to check that your ship moves at the correct rate in each part of the ocean.

Designing World Programs

During this portion of the lab we’re going to design a small "door simulator" program. Here’s how the program works:

When designing this program we’re going to ask a series of questions. You should ask yourself these questions whenever you are designing a program using big-bang.

Step 1: What stays the same?

One thing that stays the same in this program (and many others) is the images. We can define images of open, closed, and locked doors to use throughout our program. Here are some examples of what those images might look like:

open door closed door locked door

Exercise 8 Insert these images into your program and define them as constants. Use names that tell you something about the image such as DOOR-CLOSED-IMAGE.

Step 2: What changes?

The only thing changing in this program is the state of the door: whether it is open, closed, or locked. We will need to provide big-bang with a piece of data (referred to as the "world state") to describe which state we are currently in.

Remember that we want to separate data from the images of that data, so we can’t just use the images from the previous step as our data.

Instead, a good way to keep track of the state of the door would be the following partial data definition. Complete it!

; A DoorState is one of:
; - "closed"
; - "locked"
; - "open"

Step 3: Which handlers do we need?

As a recap, big-bang is our way of building interactive programs. It operates by storing a single piece of data the programmer provides, the world state, and allowing the programmer to define handler functions that modify this world state. Big-bang calls these handlers and passes them one argument (the current state of the world). In most cases, the handler function returns the next state of the world. The two exceptions to this that you will need to know are to-draw, which returns an image depicting the current world state, and stop-when which returns a boolean stating whether the big-bang program should terminate.

Most handler functions define the way the world state will change in one circumstance. For example, on-tick is called once per tick (1/28th of a second by default) and defines the way the world state will change over time. When deciding whether you need a handler such as on-tick, you must determine whether the world state will change over time with no user input. Likewise, on-mouse handles the circumstance where the user moves the mouse or clicks within the program. If this user action should change the world state, then your program needs an on-mouse handler.

As always we will need a to-draw clause. This handler is mandatory for all big-bang programs, and is called on every tick.

Based on the explanation of the program, the state of the door changes when we press a certain key. What does that tell us about the handlers we need? Remember to use the big-bang documentation for a list of what handlers are available and how to use each one.

Exercise 9 Write down the signatures of the handler functions you will need. If you are having trouble, feel free to ask a staff member to help.

Step 4: Main Function

Now that we’ve decided on our handler functions, we can put together our main function which will call big-bang. We write this function first because in this course we follow a top down programming approach. That means larger functions go at the top of our program and smaller functions go underneath.

Exercise 10 Design the main function door-simulator which, given an initial DoorState, runs the door simulation using big-bang. Recall that we cannot test functions that use big-bang because we don’t know when the user will stop the program or how they will interact with it. However, you should follow all the other steps of the design recipe for functions.

Step 5: Design your handlers

Exercise 11 Design the function that draws the DoorState. Which handler does this belong to? Remember to follow the design recipe for functions.

Exercise 12 Design the function which changes the DoorState based on user input. Which handler does this belong to? Remember to follow the design recipe for functions.

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!