instagram

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
2-AP-13

Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs

2-AP-14

Create procedures with parameters to organize code and make it easier to reuse.

2-AP-17

Systematically test and refine programs using a range of test cases

2-AP-19

Document programs in order to make them easier to follow, test, and debug.

3A-AP-17

Decompose problems into smaller components through systematic analysis, using constructs such as procedures, modules, and/or objects.

3A-AP-18

Create artifacts by using procedures within a program, combinations of data and procedures, or independent but interrelated programs.

K-12CS Standards
6-8.Algorithms and Programming.Modularity

Programs use procedures to organize code, hide implementation details, and make code easier to reuse. Procedures can be repurposed in new programs. Defining parameters for procedures can generalize behavior and increase reusability.

9-12.Algorithms and Programming.Modularity

Complex programs are designed as systems of interacting modules, each with a specific role, coordinating for a common overall purpose. These modules can be procedures within a program; combinations of data and procedures; or independent, but interrelated, programs. Modules allow for better management of complex tasks.

P4

Developing and Using Abstractions

Next-Gen Science Standards
HS-SEP5-3

Apply techniques of algebra and functions to represent and solve scientific and engineering problems.

Oklahoma Standards
OK.L1.AP.M.01

Break down a solution into procedures using systematic analysis and design.

Lesson Goals

Students will be able to…​

  • write custom helper functions to filter the animals table

  • write custom helper functions to build on the animals table

Student-facing Lesson Goals

  • Let’s practice writing functions to filter and expand our tables.

Materials

Preparation

  • Make sure all materials have been gathered

  • Decide how students will be grouped in pairs

  • Computer for each student (or pair), with access to the internet

  • Student workbook, and something to write with

  • All students should log into CPO and open the "Table Methods Starter File" they saved from the prior lesson. If they don’t have the file, they can open a new one

Language Table

Types

Functions

Values

Number

num-sqrt, num-sqr

4, -1.2, 2/3

String

string-repeat, string-contains

"hello", "91"

Boolean

==, <, <=, >=, string-equal

true, false

Image

triangle, circle, star, rectangle, ellipse, square, text, overlay, bar-chart, pie-chart, bar-chart-summarized, pie-chart-summarized

🔵🔺🔶

Table

count, .row-n, .order-by, .filter, .build-column

🔗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 (r["fixed"]), why don’t our examples?

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). CCbadge 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.