Students, having made pre-built data structures in the last lesson (autos), will generalize their understanding by defining various data structures of their own and accessing their fields. Students are introduced to Racket’s purely-functional microworld implementation. This requires an understanding of functions, data structures, and an introduction to events-based programming. To accomplish this, students first work with a simple world (a number, representing a dog’s x-coordinate). This world is consumed and produced by the update-world function, and drawn by draw-world. To understand events, they act out the World model, actually becoming event handlers and timers, to simulate a running program.
Students define two new complex data structures: party and world
Students will write functions that access fields of an auto, party, or world, and produce new autos, parties, and worlds.
Standards and Evidence Statements:
Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.
Length: 90 minutes
Glossary:
accessor functions: functions to extract values from a data structure
contract: a statement of the name, domain, and range of a function
domain: the type of data that a function expects
name: how we refer to a function or value defined in a language (examples: +, *, star, circle)
purpose statement: a brief description of what a function does
range: the type of data that a function produces
Materials:
Pens/pencils for the students, fresh whiteboard markers for teachers
Class poster (List of rules, design recipe, course calendar)
Editing environment (WeScheme or DrRacket with the bootstrap-teachpack installed)
Pens/pencils for the students, fresh whiteboard markers for teachers
Class poster (List of rules, design recipe, course calendar)
Editing environment (WeScheme or DrRacket with the bootstrap-teachpack installed)
Language Table
Preparation
Seating arrangements: ideally clusters of desks/tables
Review: the Autobody Shop
(Time 20 minutes)
In the last lesson you learned about a new kind of data struct, called an auto.
What is an auto? What things are described in an auto struct?
How do you make an auto?
How do you get the model out of an auto? The value? The color?
Last time all of these were used to make an autobody shop, where you had functions that would increase the auto’s hp, or paint it a new color. This next problem
will be even harder, so remember to refer back to the last two functions you wrote in your workbook, in case you need some hints.
Take a few minutes to review structs and autos with your students.
You may have heard of the show "Pimp My Ride", where the hosts get an old, beat-up car and make it WAY cooler. Let’s implement something like this in Racket...
Turn to Page 12 in your workbooks. Write a function called pimp, which takes in an Auto and returns a new Auto which has
double the horsepower, has 30 inch rims, is painted red, and has increased in value by $10,000.
What is the contract for this function?
For the first EXAMPLE, let’s upgrade car3. How will you start the example?
(EXAMPLE (pimp car3) ...)
According to the contract, we know that the Range of the pimp function is an auto. How do you make an auto?
What’s the first part of an auto? The model. Does the model change, according to the function’s purpose statement?
How do you get the model out of car3?
How do you get the hp out of car3?
Does the horsepower change when we "pimp" our auto? You’ll need to get the hp out of car3, and double it.
According to the purpose statement, every auto that gets pimped will have 30 inch rims. Does it matter what the original rim size was?
Likewise, every car will be painted red. Do we need to reference the original color at all?
Finally, how do you get the value out of car3? Will the value increase or decrease after the auto is upgraded?
This is an opportunity for students to practice nested expressions. Not only will they use accessor functions to access the fields of the original auto,
they will need to modify them according to the problem statement. If they get stuck, have them draw the circle of evaluation for doubling the auto’s
horsepower, adding 10,000 to the auto’s value, etc.
Putting it all together, the first example should look like:
Write one more example, circle what changes, and then define the pimp function. If you’re stuck, look back at the contract and your first example.
define-struct
Overview
Learning Objectives
Students will generalize their understanding of function constructors and accessors
Open the Autobody Shop file, and look at the first two lines at the top.
They start with ;an auto is... and define-struct.
In the last unit we skipped over the part of the code that defines the auto struct, or tells the computer what an auto is and what goes into it. Just like we would expect from having
worked with autos, the define-struct line says that an auto has five things....a model, hp, rim, color, and value. But how do we know which number is which? Remember that order
matters! Look at the order of the fields in the define-struct line. The first string is the model, the first number is the horsepower, the second number is the rim size, and so on.
You can use the given Autos file, or your students’ own files from the previous lesson. Stress the importance of being able to define your own datatypes
to students: no longer are they bound by the single values of numbers, strings, or booleans! Racket allows you to define brand new structures, containing
any combination of values. But these structures won’t be usable without the (define-struct ...) line!
A struct is defined using the define-struct function, which tells the computer what things make up that struct, and what order and type each
thing is. In return, we get new functions to use. Until we write this define-struct line, we don’t have
make-auto (to make an auto), auto-model (to get the model out of the auto), auto-hp, or any of
the other accessor functions, because Racket doesn’t know what an Auto is- we haven’t defined it.
To check this, type a semi-colon before the line which begins with define-struct. This comments it out, so that the computer ignores it. Hit run, and see what
happens. Then turn to Page 13 in your workbook, and copy
down the define-struct line.
When the define-struct line is commented out, Racket returns an error, saying you’re trying to use an identifier before its definition. That means that
it doesn’t know what make-auto is or does, because we never defined an auto struct. Make sure students understand that define-struct
is needed in order to create and work with any struct.
The Party Struct
Overview
Learning Objectives
Write complex functions that consume, modify and produce structures
Deepen their understanding of structures, constructors and accessors by being introduced to two new data structures.
Evidence Statements
Product Outcomes
Students define two new complex data structures: party and world
Students will write functions that access fields of an auto, party, or world, and produce new autos, parties, and worlds.
Materials
The Party Planner file [Party.rkt from source-files.zip | WeScheme preloaded on students’ machines
Preparation
The Party Struct
(Time 30 minutes)
Now that you know how to define your own structs, let’s define another one. Instead of working in an autobody shop, this time you’ll be a
party planner. Data structures will be a useful way to represent each party that you’re planning, keeping track of its location, theme,
and number of guests.
What datatype could be used to represent the location of the party?
What about the party’s theme? (This could be something like "50s", or "laser tag")
How about the number of guests?
Fill out the second struct definition on Page 13 in your workbook.
Open the Party Planner file. Take a look at the first two
lines in the definitions window. Do they match what you have written?
Now that the party struct is defined, you have access to four new functions: One to make a new party, and three accessor functions
to get the location, theme, and number of guests out of the party.
Turn to your contracts sheet.
What is the Name of the function that makes a party?
What is the function’s Domain? (What kinds of things are part of a party?)
What is the Range of this function?
How would you get the location out of a party? (Think about how you got the model or color out of an auto.)
Write the contracts for party-location, party-theme, and party-guests.
Now define two new party structures of your own. No matter what party you’re planning, make sure that it has the right
types of things in the right order.
As with the autos struct, repetition is key: have students identify the fields of each of their parties, and ask them lots of
questions: How would you get the theme out of Halloween? How would you get the number of guests out of JulyFourth?
Now it’s time to write some functions using the party struct. Remember, as a party planner you’ll need to be able to change
information for each party.
Turn to Page 14. Write a function called RSVP, which takes in a party and adds one to its number of
guests.
What’s the name of the function? Domain? Range?
Write the contract and purpose statement on your page.
Write your first EXAMPLE for the party Halloween. How do you start?
What does this function produce? (If you’re stuck, look back at your contract.)
Which function do we use to make a party?
According to the RSVP function, will the location of this party change? Of course not. So how do you get the location
out of a party?
What about the theme? If someone new RSVPs, do we suddenly have to make this a Christmas party? What function gets the theme
out of a party?
Lastly, what happens to the number of guests, when someone new RSVPs to the party?
The first RSVP example should be written as:
Every structure will have its own unique accessor functions for each field. Have students practice accessing each part of the Party Struct and altering them (or not!) based on the word problem.
On Page 15, write a function called relocate, which takes in a party AND the location that
it’s moving to, and makes a new party at that location. Go through each part of the design recipe: contract, examples, and definition.
Acting Out Ninja World
Overview
Learning Objectives
Discover the event-based microworld implementation of Racket, which uses events to modify the world.
Evidence Statements
Product Outcomes
Materials
Preparation
The Ninja World 1 file [NW1.rkt from source-files.zip | WeScheme preloaded on students’ machines
update-world, big-bang, and draw-world nametags
cutout image of dog
Acting Out Ninja World
(Time 30 minutes)
Do you remember the Ninja Cat game from Bootstrap 1? In this course, you’re going to completely deconstruct the game, and recreate it using
a world structure to make it more complex. This version of Ninja Cat might look a bit different than you remember:
Go back to the code and look at the line where the world structure is defined.
What function defines a struct?
What is the name of the structure defined in this file?
The world is made up of just one thing: dogX. What does dogX represent in the game?
What kind of thing is that?
Take a look at the section labelled ;; STARTING WORLD. There is a variable defined here, called
START. What kind of a thing is START? A number? String? Image? Boolean?
what function makes a world?
Skip a bit farther down to where it says ;; UPDATING FUNCTIONS. What is the name of the function
defined here? What’s its domain and range?
Think about what the update-world function is doing. How does it get the dogX out of the
world? What is it doing to the dog’s x-coordinate?
Every time update-world runs, it makes a new world, adding 10 to the dogX of the original world.
These activities encourage students to read others’ code and think about how it works, looking at the contracts and definitions and
piecing together what they already know. Ask a LOT of questions when going through the file: How do we know we need to make a new
world in update-world? (Because the range is a world). Why is dogX a good variable name for our world? Ask them to
guess what they think expressions like
(on-tick update-world)
will do in the game.
Now skip down to the last function defined in our code: big-bang. This is a special function that will begin an animation,
but it needs help from other functions to update and draw the world.
Which world is big-bang taking in?
In the code, big-bang is calling on a few different functions. What new functions can you see used in
big-bang?
The function on-tick acts kind of like a timer, and on each "tick", it updates the world. Right now the world struct is
just one number, representing the x-coordinate of the dog. (on-tick update-world) tells the computer to update
the world on every tick.
How does it do that? think back to what update-world does to the dogX of the world.
Try evaluating (big-bang START (on-tick update-world)) in to the interactions window and see what happens.
The world structure is updating, but this isn’t much of a game without images! We need to know how to draw the world.
Scroll up to where you see ;; GRAPHICS FUNCTIONS.
What is the name of the function defined here?
What is the Domain of this function? The Range?
According to the purpose statement, what does this function do?
Now look at the body of draw-world. It’s using a function you might remember from Bootstrap 1, called put-image, which takes
in an image, and then places it on top of another image, using the x- and y-coordinates to determine where to put it. In this example, it
is placing the DOG onto the BACKGROUND.
What is it using for the dog’s x-coordinate? The dog’s y-coordinate?
Once students understand the purpose of these three functions, they need to understand how they work together. Have volunteers act out
update-world and big-bang, giving them nametags with the function names on them and having them come to the board.
Have them explain to the class what their contracts are and what they do. Write: "World" on the board, with the number 0 beneath it.
When you yell "(big bang 0)", have the class start counting time, yelling "tick!" ever five seconds. On every tick, big-bang
must call on update-world to update the world written on the board. This results in the number changing over time, starting with 0.
Then have another volunteer be draw-world, giving them the "draw-world" nametag and the dog cutout. Draw a large
rectangle on the board, representing the screen. Now have big-bang call both update-world and draw-world on each "tick",
causing the number on the board to increase and the dog to move across the screen a little each time. Have the class go through a few
iterations of this. By acting out these steps, students are demonstrating exactly how the three functions work together in the code to
complete the computer animation.
Closing
Overview
Learning Objectives
Evidence Statements
Product Outcomes
Materials
Preparation
Closing
(Time 5 minutes)
If you were to act out these functions, relying on big-bang to update the world, then using the result of update-world to
draw the world, and putting them all together to create an animation, it could get tricky, and the animation wouldn’t be as fast as
what you see on the computer. Fortunately, Racket has the capability to run all these functions and more in a fraction of the time,
to create and draw a smooth, complete game. In the next few lessons, you’ll be using structs to extend this world into an actual,
complex game, and writing functions for Ninja World AND your own games to make them playable and unique.
Have students volunteer what they learned in this lesson
Reward behaviors that you value: teamwork, note-taking, engagement, etc