Students encounter String and Image datatypes and use "contracts" to make sense of the domain and range of functions.
Prerequisites |
|
Relevant Standards |
Select one or more standards from the menu on the left (⌘-click on Mac, Ctrl-click elsewhere). Common Core State Statements
Older Statements
|
Lesson Goals |
Students will be able to:
|
Student-facing Goals |
|
Materials |
|
Preparation |
|
Supplemental Resources |
|
Key Points For The Facilitator |
|
- contract
-
a statement of the name, domain, and range of a function
- datatypes
-
a way of classifying values, such as: Number, String, Image, Boolean, or any user-defined data structure
- domain
-
the type or set of inputs that a function expects
- error message
-
information from the computer about errors in code
- function
-
a mathematical object that consumes inputs and produces an output
- Image
-
a type of data for pictures
- Number
-
a data type representing a real number
- range
-
the type or set of outputs that a function produces
- String
-
a data type for any sequence of characters between quotation marks (examples: "hello", "42", "this is a string!")
Warmup
Students should be logged into WeScheme and have their Contracts page ready.
Contracts 15 minutes
Overview
This activity introduces the notion of Contracts, which are a simple notation (found in algebra) for keeping track of the set all of possible inputs and outputs for a function. They are also closely related to the concept of a function machine, which is introduced as well.
Launch
Functions are a lot like machines: values go in, something happens, and new values come out. Addition, for example, is like a machine that takes in pairs of numbers and produces a sum. Whenever we use any machine, we always think about what goes in and what comes out. A coffee maker takes in coffee beans and water, and produces coffee. A toaster takes in bread and produces toast.
We don’t have to know exactly how coffee makers or toasters work in order to use them. All we need to know is what goes in and what comes out!
We use something called a Contract to keep track of what goes in and out of functions. Contracts are like a "cheat sheet" for using functions. Once you know how to read one, you can quickly figure out how to use a function just by looking at its contract!
The Contract for a function has three parts: the Name, the Domain, and the Range
-
The Name is simply how we refer to the function:
*
,+
,sqrt
, etc. -
The Domain tells us what the function "takes in", or consumes. These also known as the arguments to the function.
-
The Range tells us what the function "gives back", produces.
-
Write the contracts for
+
,-
,*
,/
,sqr
, andsqrt
into the Contracts page.
A Sample Contracts Table
Name | Domain | Range | ||
---|---|---|---|---|
|
: |
|
-> |
|
|
: |
|
-> |
|
|
: |
|
-> |
|
|
: |
|
-> |
|
Optional: Have students turn to the Domain and Range Frayer model (Page 15) in their workbooks and use the visual organizer to explain the concepts of domain and range in their own words.
Synthesize
Knowing the Domain and Range of our functions also tells us how they can fit together. For example:
(sqrt (sqr 16))
We know from it’s Domain that the square-root function needs a number. And we know from the square function’s Range that it will produce that number. We can be pretty confident that this code will work, even before we run it.
Exploring Image Functions 25 minutes
Overview
Students explore functions that go beyond numbers, producing all sorts of simple geometric shapes and images in the process. Making images is highly motivating, and encourages students to get better at both reading error messages and persisting in catching bugs.
Launch
Students have already seen Number values like Integers, Decimals and Fractions, but computer programs can work with a much larger set of data types. Show students String values, by having them typing various things in quotation marks:
-
"hello"
-
"many words, one string"
-
"42"
-
"1/3"
-
Something students come up with on their own…
A String is anything in quotation marks. Anything. String values evaluate to themselves, the same way Numbers do.
Here are two Circles of Evaluation. One of them is familiar, but the other very different from what students have seen before. Have them identify what looks strange about that circle.
|
(star 50 "solid" "blue") |
Possible responses:
-
We’ve never seen the function
star
before -
We’ve never seen Strings used in a Circle of Evaluation before
-
We’ve never seen a function take in three inputs
-
We’ve never seen a function take in a mix of Numbers and Strings
Have students see if they can figure out the Name and Domain for the function in the second Circle. This is a chance to look for and make use of structure in deciphering a novel expression.
-
We know the name of the function is
star
, because that’s what is at the top of the circle -
We know it has three things in its Domain
-
We know the Domain consists of a Number and two Strings
But what about the Range? Have students share some guesses, and then convert the Circle to code and try it out!
-
What happened?
-
What does the
50
mean to the computer? Try replacing it with different values, and see what you get. -
What does the
"solid"
mean to the computer? Try replacing it with different values, and see what you get. -
What does the
"blue"
mean to the computer? Try replacing it with different values, and see what you get.
Error Messages The error messages in this environment are designed to be as student-friendly as possible. Encourage students to read these messages aloud to one another, and ask them what they think the error message means. By explicitly drawing their attention to errors, you will be setting them up to be more independent in the next activity! |
Suppose we had never seen star
before. How could we figure out how to use it?
-
Have students type
star
into the Interactions Area and hit "Enter". What did they get back? What do they think it means? Students learn that there is a function calledstar
. -
If it’s a function, we know that it will need an open parentheses and at least one input. Have students try
(star 50)
-
What error did we get? What hint does it give us about how to use this function?
Investigate
-
Have students turn to Exploring Image Functions.
-
Have students open a new program file and name it "Exploring Images".
Give students time to investigate image functions and see how many they can discover, using the Contracts page to organize their findings.
Strategies for English Language Learners MLR 2 - Collect and Display: As students explore, walk the room and record student language relating to functions, domain, range, contracts, or what they perceive from error messages. This output can be used for a concept map, which can be updated and built upon, bridging student language with disciplinary language while increasing sense-making. |
Synthesize
-
What image functions did you and your partner discover?
rectangle
,triangle
,ellipse
,circle
, etc. -
How did you decide what to try?
-
What error messages did you see? Input mismatches, missing parentheses, etc.
-
How did you figure out what to do after seeing an error message? Read the error message, think about what the computer is trying to tell us, etc.
Making Sense of Contracts 10 minutes
Overview
This activity digs deeper into Contracts, and has students create their own Contracts trackers to take ownership of the concept and create an artifact they can refer back to.
Launch
-
star
has three elements in its Domain: A Number, a String, and another String. What do these elements represent? The Number is the radius, the first String is the style (eitheroutline
orsolid
), the second String is the color. -
What happens if I don’t give it those things? We won’t get the star we want, we’ll probably get an error!
-
If I give
star
what it needs, what do I get in return? An Image of the star that matches the arguments -
square
has the same Domain asstar
. What do the arguments insquare
represent? length, style, color -
Can different functions have the same Domain? The same Range? Are they still different functions? Yes, yes, and yes!
-
Can we up with an example of two math functions that have the same Domain and Range?
When the input matches what the function consumes, the function produces the output we expect.
Where else have you heard the word "contract"? How can you connect that meaning to contracts in programming?
An actor signs a contract agreeing to perform in a film in exchange for compensation, a contractor makes an agreement with a homeowner to build or repair something in a set amount of time for compensation, or a parent agrees to pizza for dinner in exchange for the child completing their chores. Similarly, a contract in programming is an agreement between what the function is given and what it produces.
-
What does the contract for
star
look like?star : Number String String -> Image
Investigate
-
Students complete Domain and Range - Practice with their partner.
Students create a visual "Contracts page" either digitally or physically. Ask students to think about how they visualize contracts in their own minds and how they could use that imagery to explain functions and their contracts to others.
Additional Exercises:
-
Bootstrap:Algebra - Contracts (Quizizz)
-
Bootstrap:Algebra - Data Types & Circles of Evaluation (Desmos Activity)
-
Bootstrap:Algebra - Data Types (Desmos Activity)
-
Converting Circles of Evaluation to Code (1) (original , answers)
-
Converting Circles of Evaluation to Code (2) (original , answers)
These materials were developed partly through support of the National Science Foundation,
(awards 1042210, 1535276, 1648684, and 1738598).
BS:Games by Jen Poole, Emmanuel Schanzer, Ed Campos Jr, and Dorai Sitaram
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.