instagram

(Also available in Pyret)

Students encounter a useful representation of functions called a "Contract", which specifies the Name, Domain and Range of a function. Students learn how useful this representation is when trying to apply Functions in the programming environment, using image-producing functions to provide an engaging context for this exploration.

Lesson Goals

Students will be able to:

  • Explain the three core elements of a Contract (a set-based representation of function machines): Name, Domain, and Range

  • Use Contracts to apply functions that produce Numbers, Strings, and Images

  • Demonstrate understanding of Domain and Range and how they relate to Functions

Student-facing Lesson Goals

  • Let’s write code to make images!

  • Let’s learn to identify the Domain and Range of a function.

  • Let’s use Contracts to apply functions.

Materials

Supplemental Materials

Preparation

  • There is an optional interactive Desmos activity in the lesson. If you would like to use it, decide how you will share it with students and, if you are using our Google Slides, add the appropriate link to the slide deck. If you’re a first-time Desmos user, fear not! Here’s what you need to do.

Key Points For The Facilitator

  • Check frequently for understanding of data types and contracts during this lesson and throughout subsequent lessons.

🔗Applying Functions - Reading Contracts 15 minutes

Overview

Students learn how to apply functions in WeScheme, and are introduced to the notion of Contracts: a simple notation for keeping track of all possible input and output sets for a function.

Launch

In human languages, verbs do things to nouns. For example, I can "throw a ball", or "eat a sandwich". "Throw" and "Eat" are verbs, and "ball" and "sandwich" are nouns.

In programming languages, values are like nouns. You’ve learned about different data types in our programming language, like Number values (42, -8.3, etc), String values ("hello!"), and Boolean values (true and false).

In programming, most verbs are called functions. A function is like a machine at a factory:

  1. It takes in some values (called arguments)

  2. It does something to those values

  3. It produces a new value

Let’s play with a function to get the hang of it.

  • Log into WeScheme and click "Run".

  • Then type (string-length "rainbow") into the Interactions Area and hit Enter.

  • What do you think this function does? Come up with a theory about what string-length does, and test that theory by using it with other inputs.

Be sure to read any error messages you get back carefully! They might have clues to help you understand what’s going on.

  • What does the expression (string-length "rainbow") evaluate to?

    • 7

  • What does the function string-length do?

    • It counts the number of characters in a string and returns the value.

  • How many arguments does string-length expect? How do you know?

    • One. Giving it more than one produces an error.

  • What type of argument does the function expect? How do you know?

    • String. Giving it a different type of data produces an error.

  • What type of data did string-length produce?

    • Number

Investigate

With your partner, complete Applying Functions.

The expressions using the regular-polygon function produced a value that wasn’t a Number, a String, or a Boolean. In the WeScheme programming language, these values are called Images.

  • What are the three parts of a contract?

    • 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

  • Why is it helpful to know the Name of a function?

    • The Name tells us what to write/type, to start using the function

  • Why is it helpful to know the Domain of a function?

    • The Domain tells us how many arguments the function needs, what data type each one has to be, and in what order.

  • Why is it helpful to know the Range of a function?

    • The Range tells us what we should expect to get back from a function.

  • Where else have you heard the word "contract" used in the real world? How can you connect that meaning to contracts in programming? Answers will vary!

Contracts are binding agreements! For example, we might sign a contract with a plumber to install a sink for $200. As long as they install the sink, we are obligated to pay them $200. If a parent promises to order pizza if their child does their chores, they’d better order that pizza if the child does those chores!

If one party breaks the contract, bad things can happen. In some programming languages, breaking a function’s contract can cause the whole computer to crash, or it can result in a security hole that lets the program be hacked! In WeScheme, the contracts are checked every time to use a function, so the only result is a helpful error message.

A Contract is an agreement between us and a function: if it gets what it expects, it’ll give us what we expect.

Review students' answers for is-beach-weather and cylinder.

We’ve just encountered a lot of new vocabulary! Solidify your understanding by working through Domain and Range and/or Function and Variable to explain these ideas in your own words, using these Frayer Model visual organizers.

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.

Common Misconception

Contracts don’t tell us specific inputs. They tell us the general data type of each input that a function needs.

Contracts are general. Expressions are specific.

It would be silly for a function to only work on a single, specific input! For example, the Contract for string-length says it takes in a String, as opposed to a specific string like "rainbow". We could use any value at all…​as long as it’s a String. When writing code, however, we plug specific values into the expression we are coding. Contracts give us a big hint about what those specific values need to be.

Arguments (or "inputs") are the values passed into a function. This is different from variables, which are the placeholders that get replaced with those arguments!

Synthesize

  • How would you explain Domain and Range to someone who missed today’s class?

  • What questions do you have about Contracts?

🔗Bug Hunting 10 minutes

Overview

This activity focuses on what we can learn from error messages when a Contract is broken. The error messages in this environment are specially-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 future.

Launch

Mistakes happen, especially if we’re just figuring things out! Let’s see how error messages in WeScheme 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 Making Triangles together. We’ll be working in WeScheme.

  • Start by typing triangle into the Interactions Area, and hit the "Enter" or "Return" key to run this code.

  • What do you get back?

    • #<function:triangle>

This means that the computer knows about a function called triangle.

  • 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 (triangle 80) in the Interactions Area and read the error message.

error message for triangle 80

  • What hint does the error message give us about how to use this function?

    • triangle has three elements in its Domain. And if we don’t give it those three things we’ll get an error instead of the triangle we want.

  • We know that triangle needs 3 arguments. But what kinds of arguments are they?

  • Try different inputs to make a triangle, and see if you can figure out the Contract!

  • What is the Contract for triangle?

    • ; triangle :: Number, String, String -> Image

Investigate

Diagnosing and fixing errors are skills that we will continue developing throughout this course.

Turn to the second section of Catching Bugs when Making Triangles with your partner and try to explain the difference between syntax and contract errors in your own words.

  • What is the difference between a contract error and a syntax error?

    • syntax errors are typos (including missing quotation marks, parentheses, etc.) that prevent the computer from reading our code.

    • contract errors are when we don’t give the function the arguments it needs - either because we give it the wrong number or type of arguments.

  • Turn to the third section of Catching Bugs when Making Triangles.

  • Read each error message carefully, decide whether it’s a contract error or a syntax error and work 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 20 minutes

Overview

Students explore image functions to take ownership of the concept and create an artifact they can refer back to. Making images is highly motivating, and encourages students to get better at both reading error messages and persisting in catching bugs.

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.

Later in this lesson students will be guided through more scaffolded and in-depth reflection on isosceles-triangle, right-triangle, ellipse, rhombus, regular-polygon and radial-star. There are also optional pages for digging deeper into star-polygon, triangle-asa and triangle-sas.

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 class 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 and circle 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.

Students as Teachers

It can be empowering for students to develop expertise on a topic and get to share it with their peers! This section of the lesson could be re-framed as an opportunity for students to become experts in an image-producing function and teach their classmates about it. For example, Pair 1 and pair 4 might focus on radial-star, pair 2 and pair 5 might focus on polygon-star, pair 3 and pair 6 might focus on regular-polygon, etc. First, each pair would explore their function. Perhaps each pair could make a poster, starter-file or slide deck about their function including: the Contract, an explanation of how it works in their own words, a few images that it can generate illustrating the range of possibilities with the expressions that generate them. Next, they might compare their thinking with another pair that focused on the same Contract. Finally, pairs could be grouped with other pairs who focused on different functions and teach each other about what they learned.

  • Let’s do some more experimenting with some of the more complex image functions from the list we just saw! This time around we’ll start with their contracts.

  • Complete Triangle Contracts, Using Contracts and Radial Star with your partner.

If your students are ready to dig into more complex triangles, Triangle Contracts (SAS & ASA) will be a good challenge. Another option for further investigation is Star Polygon.

Note: Both star-polygon and radial-star generate a wide range of really interesting shapes!

If you see an error and you know the syntax is right, ask yourself these three questions:

  1. What is the function that is generating that error?

  2. What is the Contract for that function?

  3. Is the function getting what it needs, according to its Domain?

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?

🔗Composing with Circles of Evaluation 10 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 your name written on a diagonal?

  • We know that we can use the text function to make an Image of your name.

; text :: (Stringmessage, Numbersize, Stringcolor) -> Image

  • WeScheme also has a function called rotate that will rotate any Image a specified number of degrees.

; rotate :: Number, Image -> Image

But how could the rotate and text functions work together?

Investigate

One way to organize our thoughts is to diagram what we want to do, using the Circles of Evaluation. Circles of Evaluation help us think about what we want to do, without worrying about syntax like quotation marks, parentheses, etc. They let us use all our brains for thinking, before we use them for coding.

The rules are simple:

(1) Every Circle of Evaluation must have one - and only one! - function, written at the top.

(2) The arguments of the function are written left-to-right, in the middle of the Circle.

  • Values like Numbers, String, and Booleans are still written by themselves. It’s only when we want to use a function that we need to draw a nested Circle.

(3) Circles can contain other Circles!

Suppose we want to see the text "Diego" written vertically in yellow letters of size 150. Circles of Evaluation let us see the structure.

We can start by generating the Diego image.

And then use the rotate function to rotate it 90 degrees.

(text "Diego" 150 "yellow")

(rotate 90 (text "Diego" 150 "yellow"))

(text "Diego" 150 "yellow")

(rotate 90 (text "Diego" 150 "yellow"))

What do you Notice? What do you Wonder?

(text "Diego" 150 "yellow")

(rotate 90 (text "Diego" 150 "yellow"))

(text "Diego" 150 "yellow")

(rotate 90 (text "Diego" 150 "yellow"))

To convert a Circle of Evaluation into code, we start at the outside and work our way in. After each function, we write a pair of parentheses, and then convert each argument inside the Circle.

Turn to the Let’s Rotate an image of your name! section of Composing with Circles of Evaluation and practice converting the Circle of Evaluation you draw into code.

Synthesize

  • There were a lot of options for how many degrees to rotate your name in order to make it diagonal! What did you choose? Why?

  • What Numbers wouldn’t have made your name diagonal? Why?

  • What did you Notice?

  • What did you wonder?

  • Why might it be useful to separate the thinking and coding steps? Why not just do them at the same time, all the time?

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). CCbadge 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.