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, Ctrl-click elsewhere). CSTA Standards
K-12CS Standards
Next-Gen Science Standards
Oklahoma Standards
|
||||||||||||||||||
Lesson Goals |
Students will be able to…
|
||||||||||||||||||
Student-facing 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 lookup-fixed
, that would do this for us?
Fortunately, we already know how to define functions using the Design Recipe!
Turn to The Design Recipe (Page 28) 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 lookup-fixed
, 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:
# lookup-fixed :: (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!
# lookup-fixed :: (r :: Row) -> Boolean # Consumes an animal, and looks up the value in the fixed column examples: lookup-fixed(animalA) is true lookup-fixed(animalB) is false end
Step 3: Define the Function
When defining the function, we replace the answer with the lookup code.
# lookup-fixed :: (animal :: Row) -> Boolean # Consumes an animal, and looks up the value in the fixed column examples: lookup-fixed(animalA) is true lookup-fixed(animalB) is false end fun lookup-fixed(r): r["fixed"] end
No lookups in examples! In all previous functions, the examples matched the definitions almost perfectly. The only difference was the definition’s use of variables instead of actual values. So if our definition uses a lookup operation ( Data Scientists never want to stray far from the "truth", meaning they want to use real data whenever possible in order to minimize errors. So when writing examples for a specific animal, we use the actual value from that animal’s row instead of a lookup operation. |
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 28).
-
Type in the Contract, Purpose Statement, Examples and Definition into the Definitions Area.
-
Click “Run”, and make sure all your examples pass!
-
Type
lookup-sex(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 as-is. 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 29).
Suppose we want to define a function called is-cat
, which consumes a row from the animals-table
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 look-up 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"
.
# is-cat :: (r :: Row) -> Boolean # Consumes an animal, and compute whether the species is "cat" examples: is-cat(animalA) is "cat" == "cat" is-cat(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.
# is-cat :: (r :: Row) -> Boolean # Consumes an animal, and compute whether the species is "cat" examples: is-cat(animalA) is "cat" == "cat" is-cat(animalB) is "dog" == "cat" end fun is-cat(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
animals-table
. -
For practice, try solving the word problem for
is-young
at the bottom of The Design Recipe (Page 29).
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:Data Science by Emmanuel Schanzer, Nancy Pfenning, Emma Youndtsmith, Jennifer Poole, Shriram Krishnamurthi, Joe Politz, Ben Lerner, Flannery Denny, and Dorai Sitaram with help from Eric Allatta and Joy Straub 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.