Students review how to extract individual Rows from a table, then learn how to answer lookup questions by extracting a single value from a Row.
Lesson Goals |
Students will be able to…
|
Student-facing Lesson Goals |
|
Materials |
|
🔗Row and Column Lookups
Overview
Students review value definitions in Pyret, and practice extracting individual Rows for later use. They are also introduced to column lookups, which allow them to extract a specific value from a Row.
Launch
You’ve learned how to write functions that work with Numbers, Strings, and Images. But as Data Scientists, we want to write functions that work with Rows. To do that, we’ll need to take Rows apart and use the values stored in their columns.
-
Open the Row Functions Starter File and click "Run".
-
In the Interactions Area, evaluate a few of the sample rows you see defined in the file.
-
What are the names of the sample rows you see defined?
-
cat-row
,dog-row
, etc. -
What function is used to extract a row from the table?
-
row-n
-
What row would
row-n(animals-table, 1)
return? -
The second row.
Notice that we could have defined the first row from the animals table using:
sasha = row-n(animals-table, 0)
But, it’s often more useful to name our Row definitions according to the property we care about. In this case, the fact that this row is a cat is much more interesting than the fact that her name is Sasha, so the starter file uses:
cat-row = row-n(animals-table, 0)
If we were analyzing a subset of lizards, it would make sense to add a definition for lizard-row
to the Animals Dataset.
-
If you were to adopt an animal from the shelter, who would be your favorite?
-
Define a new Row called
myPet
, to be your favorite animal at the shelter.
We can access specific columns of a Row, by using a Row Accessor.
-
Row accessors start with a row value:
-
either a
row-n
expression just like the ones we’ve been working with -
or the name of a row that we have already defined
-
-
Row accessors end with square brackets
[ ]
containing the name of the column where the value can be found.
Here are three examples that use row accessors to get at different columns from the first row in the animals-table.
Without Predefined Rows | Using Predefined Rows |
---|---|
|
|
Investigate
Sometimes we want to ask questions about a specific Row, or do some computations with that Row:
-
Is the age of our
lizard-row
animal more than 3? -
How many kilograms does our
rabbit-row
weigh? -
Is the species of our
fixed-row
animal a"dog"
? -
Could we use the
age
of ourmale-row
as the size of a solid, green triangle?
We can use our knowledge of lookups, operators and functions to do just that!
-
Turn to Lookup Questions and find the
pets-table
at the top of the page. All of the questions on this page will refer back to that table. -
Complete the page, discussing your thinking with a partner.
Confirm that students have correctly completed the page before moving on. If they need more matching practice before moving on have them complete More Practice with Lookups, which focuses on the shapes-table
.
Now that we know how to lookup a value for a specific column of a row, let’s practice some of the things we might be able to do with that information!
-
Return to the Row Functions Starter File, and turn to Lookup Expressions.
-
Notice that the code at the top of the page uses the "age" of
old-row
to determine the size of the triangle. -
Type the code into the Interactions Area and test it out.
-
With a partner, work through Lookup Expressions. After you’ve written the expressions on paper, try them out in Pyret!
Have students share their most creative uses of lookups. Did anyone use three or more columns from a Row? Did anyone use more than one Row in the same expression?
Synthesize
-
Why is it helpful to be able to lookup values from a Row?
-
Once we have the values, we can use them with any Pyret expression we want!
-
We can perform computation on those values, make images, etc.
🔗Defining Functions
Overview
Students use different representations of functions to define functions that consume Rows. They also discover functions that consume other functions and compose a scatter plot function with a function they’ve defined.
Launch
By now you’ve had a chance to explore functions on your own, thinking of them in terms of several different representations:
-
A mapping between Domain and Range
-
A list of discrete input values and output values
-
A symbolic definition
Now it’s time to use those representations to help us work with Rows!
-
Open the Row Functions Starter File, save a copy, and click "Run".
-
Scroll until you see the definition for
cat-row
.
-
What will you get back if you evaluate
cat-row
in the Interactions Area? -
The first row of the table
-
Sasha’s row.
-
What species is the animal?
-
cat
-
How old is it?
-
1
Complete the top half of Functions with Lookups.
Investigate
Let’s look at an example to see how lookups can work hand-in-hand with function definitions!
Complete the age-gt
section of Functions with Lookups (the rest of the page).
If students are stuck on the examples step, here’s a useful trick to get them started.
Complete the following sentence:
"For this Row, I…"
Whatever your answer is the precise description of what to do for your example!
# CONTRACT:
# age-gt :: Row -> Image
# consumes an animal, and draws a solid green triangle whose
# size is 5x the number age of the animal as the size
examples:
age-gt(cat-row) is triangle(5 * 1, "solid", "green")
age-gt(dog-row) is triangle(5 * 3, "solid", "green")
age-gt(cat-row) is triangle(5 * cat-row["age"], "solid", "green")
age-gt(dog-row) is triangle(5 * dog-row["age"], "solid", "green")
end
fun age-gt(r): triangle(5 * r["age"], "solid", "green") end
Review student answers from the age-gt
section of Functions with Lookups.
Both pairs of examples are correct!
-
In the first pair, we see the values 1 and 3, which makes it easy for us picture the two triangles… but there’s no explanation of where the values are coming from.
-
The last pair shows how those values are looked up from the example rows, which makes it easier to write a definition that the computer can use for any row!
You can use both kinds of examples in your code! Programmers often use a mix of the two.
-
Sometimes we want to use real, concrete numbers to make sure our work is correct.
-
And sometimes we need to show all of our work, to make sure we are defining the function correctly!
Synthesize
-
Why might it be beneficial to include both kinds of examples?
-
They serve different purposes.
-
The concrete examples make our code more readable for humans and might help us to establish and verify our line of thinking.
-
And showing all the work in examples makes the pattern evident for defining the function.
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927).
Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.