instagram

(Also available in WeScheme)

Students learn to combine image transformation functions using Circles of Evaluation.

Lesson Goals

Students will be able to:

  • Use functions as building-blocks, composing them to achieve a desired affect

  • Diagram function composition using the Circles of Evaluation

  • Compose functions when programming

Student-facing Goals

  • Let’s learn to use functions as building blocks that can be combined or composed to achieve desired outcomes.

  • Let’s use Circles of Evaluation to show how functions can be composed.

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.

  • When students encounter errors, encourage them to check their contracts (which, if you’re using a printed workbook, are included in the back) and show their work using Circles of Evaluation.

🔗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:

# add1 :: Number -> Number # consumes a number, adds one, and produces the result

# double :: Number -> Number # consumes a number, and multiplies that number by 2

10

What if, starting with 4, we composed add1, double and half?

# add1 :: Num -> Num # consumes a number, adds one, and produces the result

# double :: Num -> Num # consumes a number, and multiplies that number by 2

# half :: Num -> Num # consumes a number, and produces a number that is half the input

?

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

step1 = add1(4)

(Start with 4, and now we have 5)

step2 = double(step1)

(now we have 10)

step3 = half(step2)

(now we’re back to 5)

…​or…​

(half (double (add1 4)))

half( double( add1(4) ) )

Three Steps

Three Circles

step1 = add1(4)

(Start with 4, and now we have 5)

step2 = double(step1)

(now we have 10)

step3 = half(step2)

(now we’re back to 5)

…​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 is Image -> Image. Why can we use the output of the text function as an input for flip-horizontal?

    • Because the text function produces an Image, which is then used as the input for flip-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). 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.