(Also available in WeScheme)
Students learn to combine image transformation functions using Circles of Evaluation.
Lesson Goals |
Students will be able to:
|
Student-facing Goals |
|
Materials |
|
Supplemental Materials |
|
Preparation |
|
Key Points For The Facilitator |
|
🔗Composing Functions 10 minutes
Students are given a scaffolded activity that forces them to use the output of one function as the input to another - to compose them.
Launch
-
Divide students into groups of 3 and distribute a set of Function Cards to each group. (If you’re teaching remotely you can use the Desmos version of our Function Cards instead.)
-
Write down pairs of integers on the board, representing the "starting numbers" and "ending numbers". These integers should range from -50 to +50, but you can change the difficulty of the activity by making that span wider (more difficult) or more narrow (less difficult). You can find a random integer generator here.
-
Have students practice reading contracts by asking them questions about one or two of these cards.
-
Pick a card at random.
-
According to its Contract, what is the function’s Name, Domain, and Range?
These cards represent a collection of functions, each of which takes an input and produces an output. For example, we can start with the number 4
, and give it to the function add1
. The output will be 5
.
We can also compose functions, meaning that the output of one is immediately passed into another.
Function composition is the act of connecting two or more functions so that they work together to produce an outcome. The output ("range") of one function is used as the input ("domain") of the next.
For example, let’s say we are going to compose two different functions: add1
and double
. Passing 4
into the add1
function produces 5
. Passing that output into double
produces an output of 10
:
|
⟶ |
|
⟶ |
10 |
What if, starting with 4
, we composed add1
, double
and half
?
|
⟶ |
|
⟶ |
|
⟶ |
? |
Investigate
-
For each pair of numbers on the board, figure out which functions to compose to get from the starting number to the ending number.
-
You will need to use some functions more than once, and that’s okay!
Give students time to experiment with this!
To make the activity more challenging, ask students to find the shortest path from the starting number to the ending number, using the smallest number of compositions. You can also show students a starting number and an ending number and: ask them to give a card back; ask them which cards are the most valuable; give them a blank card and let them come up with a new function.
There are two ways of thinking about the activity you just completed.
-
Option 1: Each function produces a result. The result is saved, and then used as the input for the next function.
-
Option 2: Each function passes its result directly into the next function.
Three Steps |
Three Circles |
|||||||
|
…or… |
(half (double (add1 4)))
half( double( add1(4) ) ) |
-
Which representation (above) corresponds with Option 1? How do you know?
-
The table. The steps occur one by one, in sequence. Each step is defined with a name (step1, step2, etc). We compute several intermediate outcomes before arriving at the final outcome.
-
-
Which representation corresponds with Option 2?
-
The Circle of Evaluation. All of the nested circles form a single unit.
-
Some students may notice that both representations use function notation. If your students are confident with function notation, you can make the connection here. Otherwise, students have the opportunity to think deeply about function notation in this lesson.
Synthesize
Did it matter what order you put the cards in? Why or why not?
If two groups come up with different compositions that achieve the same end result, have them share their ideas!
🔗Composing Functions in Code 20 minutes
Overview
The Circles of Evaluation are extended to functions that do more than compute values.
Launch
The functions available to us in Pyret can be composed just like the Function Cards from the activity. Our job as programmers is to figure out how to compose functions to achieve our goals in the most clever or elegant way possible.
Investigate
-
Have students open code.pyret.org (CPO) in their browser, and "Sign In" using a valid Google account (Gmail, Google Classroom, YouTube, etc.) and their password for that account.
-
This will take them to the "Programs" page. This page is empty - they don’t have any programs yet!
-
Have them open a new program by clicking "File" -> "New" and save it as "Function Composition"
-
Complete Function Composition — Green Star, in which you will draw circles of evaluation to help you write expressions to compose a series of images.
-
Be sure to use the Definitions Area (left side) for code you want to keep and the Interactions Area (right side) to test code or try out new ideas.
When students are finished, check their work, and ask them to change the color of all of the stars to “gold” or another color of their choosing.
Now, turn to Function Composition — Your Name in which you will create a text image of your name and experiment with other functions.
Facilitation Note
While students are exploring, be available for support but encourage student discussion to solve problems. Many student questions can be addressed with these responses: Did you try drawing the Circle of Evaluation first? Did you check the contract? Have you pressed the "Run" button to save your Definitions changes?
Encourage students to practice writing comments in the code to describe what is being produced, using #
at the beginning of the line.
If you have time, you can have students work with Function Composition — scale-xy and/or Function Composition Matching Activity (Desmos) - or use either one for assessment!
Optional Project: Create Your Own Logo
Extend and solidify student understanding of function composition by challenging your students to create their own logos! Project: Create Your Own Logo walks students through the process of designing and building a personal design or logo.
Synthesize
-
What do all of these functions have in common?
-
They all produce images, they all change some element of the original image_
-
-
Does using one of these functions change the original image?
-
No, it creates a whole new image
-
-
What does the number in
scale
represent?-
The scale factor by which the image should grow or shrink
-
-
What does the number in
rotate
represent?-
The rotation angle, measured counterclockwise
-
-
The Domain and Range for
flip-horizontal
isImage -> Image
. Why can we use the output of thetext
function as an input forflip-horizontal
?-
Because the
text
function produces an Image, which is then used as the input forflip-horizontal
.
-
Fun with Images!
Now that students have learned how to use all of these image-composing functions, you may want to give them a chance to create a design of their own, tasking them with using at least 4 functions to create an image of their choosing.
Our Transforming and Composing Images and Project: Make a Flag also dive deeper into image composition.
🔗Composing Multiple Ways Optional
Overview
Students identify multiple expressions that will create the same image, and think about the merits of one expression over another.
Launch
As is often true with solving math problems, there is more than one way to get the same composed image.
Suppose I wrote the code: scale(3, circle(50, "solid", "red"))
.
-
What’s another line of code I could write that would produce the exact same image?
-
circle(150, "solid", "red")
-
Investigate
There is a special function that lets us test whether or not two images are equal:
# images-equal :: Image, Image -> Boolean
We can use it to test whether the expressions we wrote really build the same images.
Synthesize
-
Could we have written more expressions to create the same images?
-
Are all of the ways to write the code equally efficient?
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.