Students learn about functions for sorting and counting data in tables, then are introduced to one-variable displays.
Lesson Goals |
Students will be able to…
|
Student-facing Lesson Goals |
|
Materials |
|
Supplemental Materials |
|
🔗Functions for Working with Tables 20 minutes
Overview
Students will be introduced to functions for working with tables, including: sort
, count
and first-n-rows
. In the process they will review simple data types and be introduced to contracts for describing the Domain and Range of any function.
Launch
-
Open the Animals Starter File and click "Run".
-
In the Interactions Window on the right type
animals-table
and hit "Enter" to see the the table.-
What do you Notice?
-
Sample response: We only see the first 10 rows of the table and would have to click to see the remaining 22 rows.
-
The animals aren’t listed in any particular order.
-
-
What do you Wonder?
-
Sample response: Is it possible to alphabetize the list, or sort it in other ways?
-
-
If this is the first time your students are seeing a table in code.pyret.org (CPO), you may also want to acknowledge lines 7-9 of the Definitions Area, where animals-table
is defined along with the names of the 8 columns.
Investigate
Turn to Functions for Tables and complete numbers 1-6.
-
What did these
sort
expressions do?-
They took in the
animals-table
, the "Name" column, and true or false… producing a new table, sorted alphabetically (A-Z for true and Z-A for false)
-
-
Just below question 6, we see the Contract for
sort
-
What does it mean that the Domain of
sort
is Table, String, Boolean?-
The functions needs a Table, a String (describing a column), and a Boolean (an order to sort the column by) in order to do its job.
-
-
What does it mean that the Range of
sort
is Table?-
The function produces a new table.
-
As you can imagine, there are many other functions that work with tables, each with a different set of inputs. For each of these functions, we need to keep track of three things:
-
Name — the name of the function, which we type in whenever we want to use it
-
Domain — the type(s) of data we give to the function
-
Range — the type of data the function produces
The Name, Domain and Range are used to write a Contract.
Contracts are the instruction manual for functions.
The Domain in the Contract for sort
tells us exactly what kind of inputs it needs, and in what order. The Range tells us exactly what we’ll get back.
Complete question 7-10 on Functions for Tables.
-
How did
sort
work with quantitative columns?-
true sorted the values from least to greatest. false sorted the values from biggest to smallest.
-
-
What new questions did you think of, which could be answered using
sort
?
-
Once you know how to read Contracts, you can easily use all the tools in our programming language.
-
Let’s explore the
count
andfirst-n-rows
functions, by completing Functions for Tables (continued).
Common Misconceptions
Students are likely to think that sort
changes the table, when instead it produces a new, sorted table. Encourage students to say out loud what they think they will get if they type animals-table
after evaluating sort(animals-table, "name", true)
. By testing their hypothesis, students who are surprised at the outcome are much more likely to remember the difference later on.
Synthesize
-
How is the function of
count
different fromsort
?-
sort
made a new table that was reordered_ -
count
made a new table that summarized the data from a column of the original table!
-
-
How does the function
first-n-rows
work?-
It makes a new smaller table with the number of rows you type into the expression.
-
-
Do you have any questions about the functions or expressions you’ve worked with today?
-
Where have you seen tables summarizing counts in the real world?
-
How else do journalists and newscasters display summaries of data besides in tables?
-
Ideally someone will say bar charts!
-
🔗Composing with Circles of Evaluation 15 minutes
Overview
Students learn to work with more than one function at once, by way of Circles of Evaluation, a visual representation of the underlying structure.
Launch
What if we wanted to see the ten youngest animals?
-
How could the
first-n-rows
andsort
functions work together? -
What order should we use the functions in?
Investigate
One way to organize our thoughts is to diagram what we want to do, using the Circles of Evaluation.
The rules for Circles of Evaluation are simple:
(1) Every Circle of Evaluation must have a single function, written at the top.
(2) The arguments of the function are written left-to-right, in the middle of the Circle.
If we want to see the first ten animals, our diagram would look like this.
(3) Circles can contain other Circles!
If we want to see the ten youngest animals, our diagram would look like this.
If we wanted to get extra fancy and see the species count for the youngest ten animals, we could add another layer to our Circle of Evaluation.
Note: Values like Numbers, Strings, and Booleans are still written by themselves. It’s only when we want to use a function that we need to draw a Circle.
To convert a Circle of Evaluation into code:
-
We start at the outside and work our way in.
-
After each function we open a pair of parentheses, and then convert each argument inside the Circle.
-
We close each pair of parentheses as we finish with the arguments in its Circle of Evaluation.
This diagram would translate to the code that follows.
And this diagram would translate to the code that follows.
-
Draw Circles of Evaluation and write code for each of the given scenarios.
-
Then test your code out in Animals Starter File.
Synthesize
-
What did you Notice?
-
What did you wonder?
🔗Bug Hunting 10 minutes
Overview
This activity focuses on what we can learn about Pyret functions from the messages we get back in the Interactions Area. The error messages in this environment are specially-designed to be as student-friendly as possible. By explicitly drawing their attention to errors, you will be setting them up to be more independent in the future.
Launch
Let’s see how error messages in Pyret can help us to figure out the contract for a function we’ve never seen before.
-
Let’s complete the first section of Catching Bugs when Sorting Tables together. We’ll be working in Animals Starter File.
-
Start by typing
sort
into the Interactions Area, and hit the "Enter" or "Return" key to run this code.
-
What do you get back?
-
<function:sort>
-
This means that the computer knows about a function called
sort
.
-
-
We know that all functions will need an open parentheses and at least one input!
-
We don’t know the Domain, so we don’t know how many inputs or what types they are. But we can always guess, and if we get it wrong we’ll use the error message as a clue.
-
Type
sort(animals-table)
in the Interactions Area and read the error message.
-
What hint does the error message give us about how to use this function?
-
-
The the sort function expects 3 arguments and its Domain is Table, String, Boolean. If we don’t give it those three things we’ll get an error instead of the sorted table we want.
-
Investigate
Mistakes happen, especially if we’re just figuring things out! Diagnosing and fixing errors are skills that we will continue developing throughout this course.
-
Turn to the second section of Catching Bugs when Sorting Tables with your partner and try to explain the difference between syntax and contract errors in your own words.
-
Then turn to the third section of Catching Bugs when Sorting Tables. Read each error message carefully, decide whether it’s a contract error or a syntax error and work together to decipher what it’s trying to tell us.
Synthesize
-
What kinds of syntax errors did you find?
-
What kinds of contract errors did you find?
🔗Exploring Image Functions 10 minutes
Overview
Making images is a highly motivating context for reading error messages and writing contracts.
-
If you have time and interest in digging deeper we have a whole lesson focused on contracts for image-producing functions, with many scaffolded pages of additional practice.
-
If you have already completed our Contracts lesson with your class, skip to the next section of this lesson.
Launch
-
Turn to Contracts for Image-Producing Functions and find
triangle
. -
You’ll see that both the contract and a working expression have been recorded for you.
-
Take the next 10 minutes to experiment with trying to build other shapes using the functions listed.
-
As you figure out these functions, record the contracts and the code!
Supporting Diverse Learners
Image exploration is a low threshold / high-ceiling activity that should be engaging to all students. Do not try to keep your students in lock-step. Some students may find the contracts for all of these functions, but most students will not! What is important here is for everyone to have the opportunity to explore.
Students do not need to find all of the contracts on this page in order to complete the lesson or the following pages.
In order to make sure that all students both remain engaged and are prepared to engage in productive class discussion, when you become aware that the first student in your class has successfully used the text
function, give your students directions about which functions to prioritize with the remaining time.
Make sure students at least find the contracts for star
, rectangle
and text
before moving ahead.
Investigate
-
Does having the same Domain and Range mean that two functions do the same things?
-
No! For instance,
square
,star
,triangle
andcircle
all have the same Domain and Range, yet they make very different images because they have different function Names, and each of those functions are defined to do something very different with the inputs!
-
-
What error messages did you see?
-
Error messages include: too few / too many arguments given, missing parentheses, etc.
-
-
How did you figure out what to do after seeing an error message?
-
Reading the error message and thinking about what the computer is trying to tell us can inform next steps.
-
Synthesize
-
A lot of the Domains for shape functions are the same, but some are different. Why did some shape functions need more inputs than others?
-
Was it harder to find contracts for some of the functions than others? Why?
-
How was it different to code expressions for the shape functions when you started with a Contract?
🔗Functions for Making Displays 20 minutes
Overview
Students will be introduced to functions for making one-variable displays in Pyret, including: pie-chart
, bar-chart
, box-plot
and histogram
.
The goal here is for students to become familiar with using Contracts to write expressions that will produce displays. But knowing how to make a histogram doesn’t mean a student really understands histograms, and that’s OK!
Once students know how to use Contracts to write expressions to make these displays, we have dedicated, in-depth lessons focused on understanding Bar and Pie Charts, Histograms, Visualizing the "Shape" of Data, Box Plots, Scatter Plots, Linear Regression, Advanced Displays, etc.
Launch
The count
function summarized the data for a single variable in a new table.
The same information could be communicated as a picture! This is called data visualization, and Pyret has functions that can make displays for us!
Investigate
Turn to Exploring Displays. Let’s look at the first function together.
-
What is the name of the function?
-
bar-chart
-
-
What is the Domain of the function?
-
Table, String
-
-
What is the Range of the function?
-
Image
-
-
Take a minute and see if you and your partner can write an expression that will generate a
bar-chart
. -
Did
bar-chart
consume a categorical or quantitative column of data?-
categorical
-
-
What does the resulting display tell us?
-
Make a sketch of the display you just built in Pyret.
-
Then work to complete Exploring Displays, generating each of the other 3 displays. Some of them may be new to you - you are not expected to be an expert in them yet, but you should be able to figure out how to use the Contract to get them building!
If your students are already familiar with scatter plots, linear regression plots, and line graphs, you may also want to have them complete Exploring Displays (2).
Just as we can use Circles of Evaluation to help us combine sort
, count
, and first-n-rows
, we can put Circles of Evaluation to work to help us write code to build more specific displays. Consider this:
-
What expression would this Circle of Evaluation generate?
-
-
What would be the resulting display?
-
a pie chart showing the species of the 10 youngest animals
-
If your students would benefit from seeing a few more examples before drawing their own Circles of Evaluation, have them complete Composing Functions: Match Display Descriptions to Circles of Evaluation.
-
Complete Circles of Evaluation: Composing Functions to Make Displays.
-
Then consider what display it might be interesting to compare each of the displays on this page with.
-
Displays are often most informative when compared with other displays.
-
For example, we may want to see how the age range of the animals adopted quickly compares to the age range of all the animals or of the animals that were adopted slowly.
-
For more practice making tables and displays by composing functions, have students complete Circles of Evaluation: Composing Functions to Make Displays (2)
Synthesize
-
Which displays worked with categorical data?
-
pie-chart
andbar-chart
-
-
Why might you choose a bar chart over a pie chart or vice versa?
-
pie-chart
only makes sense when you have the full picture, since it’s representing the proportion of the whole -
bar-chart
shows the count
-
-
How are bar charts and histograms different?
-
bar-chart
summarizes categorical data. Each bar represents the count of a specific category. -
histogram
displays the distribution of quantitative data across the range.
-
🔗Additional Exercises
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.