Students continue practicing the Design Recipe, writing helper functions to filter rows and build columns in the Animals Dataset, using Methods.
Prerequisites 

Relevant Standards 
Select one or more standards from the menu on the left (⌘click on Mac, Ctrlclick elsewhere). CSTA Standards
K12CS Standards
NextGen Science Standards
Oklahoma Standards


Lesson Goals 
Students will be able to…


Studentfacing Lesson Goals 


Materials 

Preparation 


Language Table 

🔗Defining Lookup Functions 25 minutes
Overview
Students continue practicing the Design Recipe, by writing functions to answer Lookup Questions.
Launch
Take two minutes to find all the fixed animals by hand. Turn to The Animals Dataset, and walk down the table one row at a time, putting a check next to each animal that is fixed.
To do this activity, what kind of question were you asking of each animal? Was it a Lookup, Compute, or Relate question?
You went through the table one row at a time, and for each row you did a lookup on the fixed
column.
Have students type the code that will look up if animalA
is fixed or not, then do the same with animalB
. Suppose we wanted to do this for every animal in the table? This seems really repetitive, doesn’t it? We would keep typing the same thing over and over, but all that’s really changing is the animal. Wouldn’t it be great if Pyret had a function called lookupfixed
, that would do this for us?
Fortunately, we already know how to define functions using the Design Recipe!
Turn to The Design Recipe (Page 63) in your Student Workbook.
Step 1: Contract and Purpose
The first thing we do is write a Contract for this function. You already know a lot about contracts: they tell us the Name, Domain and Range of the function. Our function is named lookupfixed
, and it consumes a row from the animals table. It looks up the value in the fixed
column, which will always be a Boolean. A Purpose Statement is a description of what the function does:
# lookupfixed :: (r :: Row) > Boolean # Consumes an animal, and lookup the value in the fixed column
Since the contract and purpose statement are notes for humans, we add the # symbol at the front of the line to turn it into a comment. Note that we used "lookup" in the purpose statement and the function name! This is a useful way of reminding ourselves what the function is for.
Be sure to check students’ contracts and purpose statements before having them move on.
Step 2: Write Examples
Writing examples for Lookup questions is really simple: all we have to do is look up the correct value in the Row, and then write the answer!
# lookupfixed :: (r :: Row) > Boolean # Consumes an animal, and looks up the value in the fixed column examples: lookupfixed(animalA) is true lookupfixed(animalB) is false end
Step 3: Define the Function
When defining the function, we replace the answer with the lookup code.
# lookupfixed :: (animal :: Row) > Boolean # Consumes an animal, and looks up the value in the fixed column examples: lookupfixed(animalA) is true lookupfixed(animalB) is false end fun lookupfixed(r): r["fixed"] end
Investigate
For practice, try using the Design Recipe to define another lookup function.

Use the Design Recipe to solve the word problem at the bottom of The Design Recipe (Page 63).

Type in the Contract, Purpose Statement, Examples and Definition into the Definitions Area.

Click “Run”, and make sure all your examples pass!

Type
lookupsex(animalA)
into the Interactions Area.
🔗Defining Compute Functions 25 minutes
Overview
Students define functions that answer Compute Questions, again practicing the Design Recipe.
Launch
We’ve only been writing Lookup Functions: they consume a Row, look up one column from that row, and produce the result asis. And as long as that row contains Boolean values, we can use that function with the .filter
method.
But what if we want to filter by a Boolean expression? For example, what if we want to find out specifically whether or not an animal is a cat, or whether it’s young? Let’s walk through an example of a Compute Function using the Design Recipe, by turning to The Design Recipe (Page 64).
Suppose we want to define a function called iscat
, which consumes a row from the animalstable
and returns true if the animal is a cat.

Is this a Lookup, Compute or Relate question?

What is the name of this function? What are its Domain and Range?

Is Sasha a cat? What did you do to get that answer?
To find out if an animal is a cat, we lookup the species column and check to see if that value is equal to "cat"
. Suppose animalA
is a cat and animalB
is a dog. What should our examples look like? Remember: we replace any lookup with the actual value, and check to see if it is equal to "cat"
.
# iscat :: (r :: Row) > Boolean # Consumes an animal, and compute whether the species is "cat" examples: iscat(animalA) is "cat" == "cat" iscat(animalB) is "dog" == "cat" end
Write two examples for your defined animals. Make sure one is a cat and one isn’t!
As before, we’ll use the pattern from our examples to come up with our definition.
# iscat :: (r :: Row) > Boolean # Consumes an animal, and compute whether the species is "cat" examples: iscat(animalA) is "cat" == "cat" iscat(animalB) is "dog" == "cat" end fun iscat(r): r["species"] == "cat" end
Don’t forget to include the lookup code in the function definition! We only write the actual value for our examples!
Investigate

Type this definition — and its examples! — into the Definitions Area, then click “Run” and try using it to filter the
animalstable
. 
For practice, try solving the word problem for
isyoung
at the bottom of The Design Recipe (Page 64).
Synthesize
Debrief as a class. Ask students to brainstorm some other functions they could write?
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap:Integrated Oklahoma by Jen Poole is licensed under a Creative Commons 4.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available by contacting schanzer@BootstrapWorld.org.