imageUnit 4
The Design Recipe

Unit Overview

Students are introduced to the Design Recipe and apply it to simple problems.

Learning Objectives:

  • Students practice using Contracts to create examples of functions

  • Students learn to abstract over examples to create functions

Product Outcomes:

  • Students will use the Design Recipe to define a function, which is used to make a rocket fly.

  • Students will write functions to solve simple problems by using the Design Recipe

Standards:

See our Standards Document provided as part of the Bootstrap curriculum.
  • A-CED.1-4: Create equations that describe numbers or relationships

  • F-IF.1-3: Understand the concept of a function and use function notation

  • F.IF.7-9: Analyze functions using different representations

  • F-BF.1-2: Build a function that models a relationship between two quantities

Length: 95 minutes

Glossary:

  • contract: a statement of the name, domain, and range of a function

  • domain: the type of data that a function expects

  • example: shows the use of a function on specific inputs and the computation the function should perform on those inputs

  • name: how we refer to a function or value defined in a language (examples: +, *, star, circle)

  • purpose statement: a brief description of what a function does

  • range: the type of data that a function produces

  • variable: something that changes

Types

Functions

Number

+ - * / sq sqrt expt

String

string-append string-length

Image

rectangle circle triangle ellipse star text scale rotate put-image

Review

Overview

Learning Objectives

    Product Outcomes

      Standards

        Materials

        • Pens/pencils for the students, fresh whiteboard markers for teachers

        • Class poster (List of rules, language table, course calendar)

        • Language Table (see below)

        Preparation

          Review (Time 15 minutes)

          The Design Recipe

          Overview

          Students are formally introduced to the steps of the Design Recipe.

          Learning Objectives

          • Students practice using Contracts to create examples of functions

          • Students learn to abstract over examples to create functions

          Evidence Statements

          • Given a contract and purpose statement for a simple, one-variable function, students will be able to write two Examples

          • Given two examples for a simple function, students will be able to identify the variable

          • Given two examples for a simple function, students will be able to write the definition

          Product Outcomes

          • Students will use the Design Recipe to define a function, which is used to make a rocket fly.

          Standards

          • A-CED.1-4: Create equations that describe numbers or relationships

          • F-IF.1-3: Understand the concept of a function and use function notation

          • F.IF.7-9: Analyze functions using different representations

          • F-BF.1-2: Build a function that models a relationship between two quantities

          Materials

          • Pens/pencils for the students, fresh whiteboard markers for teachers

          • Class poster (List of rules, language table, course calendar)

          • Language Table (see below)

          Preparation

          • Write agenda on board

          • Display Class posters, Language Table, Design Recipe

          • "Rocket" [Rocket.rkt from source-files.zip | WeScheme] preloaded on students’ machines

          • Seating arrangements: ideally clusters of desks/tables

          The Design Recipe (Time 25 minutes)

          • Functions are a key part of animation in computer programs. A function that draws a static picture of a cat, for example, can place the cat at a different location based on the input. When that input changes slightly based on time or user-interaction, the cat will appear to move. This is similar to the way that flip-book animations work, in which each page draws a static image that has changed by a small amount. When the pages are displayed quickly, the images appear to change smoothly. (Click here for an example of a flip-book animation.).

          • Let’s see an example of this kind of animation, using a function to make a rocket-blast off! Turn to Page 11 in your workbook, and read the word problem carefully.
            • What is the rocket-height function taking in as input? What type of data is that?

            • What is the function producing as outnput? What type of data is that?

            • What are the three parts of a Contract?

            • What is the Name of the function you are being asked to define?

            • What is the Domain of the function?

            • What is the Range of the function?

            Check student understanding carefully, to make sure students read the problem carefully. It may be helpful to draw a diagram or table showing the change of rocket position on the board, and to have students verbally walk through a few examples.

          • The Contract is a way of thinking about the function in a general way, without having to worry about exactly how it will work or how it will be used. Starting with simple questions like these will make later steps much easier to think about. However, the Contract doesn’t always enough information! The Domain for star, for example, specifies that the function needs a Number and two Strings, but doesn’t mention the fact that the first String must be "solid" or "outline". To add this information, programmers write Purpose Statements, which are simple sentences that explain what a function does.

            Underneath the Contract, copy the following simple Purpose Statement for rocket-height.

            This is an opportunity to talk about the importance of writing, clarity, and brevity. What information is essential for a purpose statement? What information is irrelevant? A good purpose statement describes what is computed and how its inputs are used; it should go beyond the information given in the contract and implicit in the name of the function.

          • Armed with the Contract and Purpose Statement, it becomes easy to write an EXAMPLE. Every example begins with the name of the function and a sample input, both of which are written in the Contract. In this case, you know that the function is called rocket-height and that it expects a single number as input. The Purpose Statement goes further, telling you that the input is multiplied by 7. We can use this two write two examples, with different numbers of seconds as inputs. Note: The example shown above is broken into two lines! As functions and examples become more complex, it will become difficult to squeeze them into a single line. In your workbooks, every example and definition from this point onwards will use the two-line format.

            In your workbook, write two new examples of rocket-height using different values for the number of seconds. Both examples will have a lot in common (they all use rocket-height as the function name, they all multiply their input by 7, etc). Once you are done writing them, circle only the parts of the examples that are changeable.

            The main idea for students in this activity is to realize that the Contract and Purpose Statement can be relied upon to write examples. Ask students to justify every part of each example, using only the Contract and Purpose Statement. Students are often tripped up by the switch from one-line examples to two-line ones. Be very careful to point this out to students, and to check their workbooks to see where they are writing their examples. At the end of the activity, make sure that students circle ONLY what has changed.

          • By comparing two different examples, it is easy to tell what changes. According to the Purpose Statement, it is the number of seconds that the rocket has been flying.

            On your paper, label the items you circled with "seconds".

            Have a discussion with students about why "seconds" is a better name than "time". Talk about specificity, relevance, and readability.

          • Labeling what is changeable gives programmers a sense for the names of a function’s variables.

            How many variables does rocket-height have? What is the name of each variable? Define the function, using all the information from your Examples, Contract and Purpose Statement.

            As with the Examples, ask students to justify each part of the definition. In this case, the function name can be derived from the Contract, and the variable name and function body from the Examples.

          • The Design Recipe allows a programmer to focus on one step of the problem at a time, and to use previous steps to help complete the next one.
            • What does the Contract tell a programmer about a function?

            • What does the Purpose Statement tell a programmer about a function?

            • How do the Contract and Purpose Statement help a programmer write Examples?

            • Why is it helpful to circle and label the parts of the Examples that change?

            • How do all of these steps help a programmer define a function?

          • You may have noticed that the Examples for rocket-height wrote out the multiplication ((* 11 7)), rather than the actual answer (77). Why bother to show the way a calculation is performed? By doing this, Examples can provide hints about what process is taking place. In the provided Rocket code ([Rocket.rkt from source-files.zip | WeScheme]), you will see why it is so important to show your work when writing examples.

            Click "Run", and wait until a window appears with a rocket at the bottom of the screen and numbers for "time" and "height" at the top. This animation is set to update the rocket every time the spacebar is pressed, to simulate time going by. Hit the spacebar a few times, and notice that the time at the top of the window increases. Does the rocket move? Time is passing, but our rocket’s height hasn’t changed! Close the rocket window, so that you can see the code.

          • The Contract for this function is correct: the function’s Name, Domain and Range are all accurately written here. However, the next step definitely has some problems:

            1. There is only one Example. This makes it difficult to notice what is changing, which could lead to mistakes when identifying variables

            2. The Example doesn’t show how the height is calcluated - instead, this programmer just wrote the "answer", without showing their work.

          • By skipping these steps in the Examples, it can be easy to make mistakes when defining the function.

            Can you see the mistake the programmer made?

            Without seeing multiple Examples, this programmer failed to realize that the height of the rocket has to be calculated for every input. Instead, they just produce the same number every time (in this case, zero). As a result, the rocket is always at zero, no matter how many seconds have passed.
            • Fix this programmer’s Example to show their work.

            • Write a second Example, below the first one.

            • Fix the definition for the function, so that it multiplies the number of seconds by 7.

            • Click "Run", and then use the spacebar to make the rocket fly!

          Design Recipe Practice

          Overview

          Students use the Design Recipe to define simple functions (of one and two variables), given various word problems

          Learning Objectives

            Evidence Statements

            • Given a contract and purpose statement for a simple function, students will be able to write two Examples.

            • Given two examples for a simple function, students will be able to identify the variable.

            • Given two examples for a simple function, students will be able to write the definition.

            Product Outcomes

            • Students will write functions to solve simple problems by using the Design Recipe

            Standards

              Materials

              • Pens/pencils for the students, fresh whiteboard markers for teachers

              • Class poster (List of rules, language table, course calendar)

              • Language Table (see below)

              Preparation

              • Write agenda on board

              • Display Class posters, Language Table, Design Recipe

              • Seating arrangements: ideally clusters of desks/tables

              Design Recipe Practice (Time 50 minutes)

              • Remember that the Design Recipe is a roadmap for defining functions, which programmers use to make sure the code they write does what they want it to do. Each step builds on the last, so any mistakes can be caught early in the process.

                Turn to Page 12 in your workbook, and read the word problem carefully.
                • Circle the Name of the function, and underline what it takes in and what it produces.

                • Write the Contract for this function, using what you underlined to help you find the Domain and Range.

                • Underneath the Contract, write the Purpose Statement by summarizing what the function does in a single sentence

                Challenge students to explain why this function does not need to know the color of the square, or whether or not it is solid. They main idea here is that the function already "knows" these things, so the only thing that is changing is the size of the square.

              • Remember that the Contract and Purpose Statement can be used to write the Examples, even if a programmer isn’t sure how to begin.

                Using only the Contract and Purpose Statement, see if you can answer the following questions:
                • Every Example begins with the name of the function. Where could you find the name of the function?

                • Every Example has to include sample inputs. Where could you find out how many inputs this function needs, and what types they are?

                • Every Example has to include an expression for what the function should do when given an input. Where could you look to find out what this function does?

                • Write two Examples on your paper, then circle and label what is changing between them.

                Make sure students realize that every answer can be found in the Contract or Purpose Statement. Suggestion: write these steps on the board, and draw arrows between them to highlight the process. The goal here is to get students into the habit of asking themselves these questions each time they write Examples, and then using their own work from the previous step to find the answers. NOTE: students can solve this using either square or rectangle. The latter requires them to use the same variable twice, which is a useful thought-exercise.

              • Once the two Examples are written and the variable (or variables) are labeled, it becomes easy to define the function.

                Using the Contract, Purpose Statement and Examples, see if you can answer the following questions:
                • Every function definition begins with the name. Where did you write this already?

                • Every definition has to include variable names for the inputs. Where did you write these down?

                • Every definition should work exactly the way the Examples did. Where could you look to figure out what the function should do?

                • Write the function definition on your paper.

              • When typing in a function definition, be sure to type in all three parts: The Contract and Purpose Statement, the Examples and the Definition. When "Run" is clicked, the computer will read the definition, test your Examples to make sure it works correctly, and then add the definition to your language. Once added, you can use the function as if it were built into the computer.

                Test out your function by using it with various inputs.

                ; red-square : Number -> Image ; draw a solid, red square using the given size (EXAMPLE (red-square 42) (square 42 "solid" "red")) (EXAMPLE (red-square 73) (square 73 "solid" "red")) (define (red-square size) (square size "solid" "red"))

              • Some functions take more than one input. The red-square function always makes solid red squares, with size being the only thing that varies. Suppose, however, we wanted the function to make red squares that could be either solid or outline? This would mean that both the size and the style can vary, so a user might type (red-square 50 "solid") or (red-square 50 "outline")

                • How would that change the Domain of the function?

                • How would the Examples have to change?

                • How would the definition change?

                Change each part of the code you typed, to allow red-square to take in "solid" or "outline" as a second argument.

              • The Design Recipe can be used for functions that take any number of inputs. As a simple example, take the following word problem:

                Turn to Page 13 in the workbook, and read the word problem carefully.
                • Circle the Name of the function, and underline what it takes in and what it produces.

                • How many things does this function take as input? Are they Numbers? Strings? Images?

                • Write the Contract for this function, using what you underlined to help you find the Domain and Range.

                • Underneath the Contract, write the Purpose Statement by summarizing what the function does in a single sentence.

                • Write two Examples for this function, then circle and label what varies between them.

                • Write the Definition for this function.

                If time allows, pass out extra Design Recipe worksheets [PDF | Ooo], and have students practice with other word problems. Practice variations on red-square, involving two inputs changing while other parameters stay fixed.

              Closing

              Overview

              Learning Objectives

                Product Outcomes

                  Standards

                    Materials

                      Preparation

                        Closing (Time 5 minutes)

                        • In this unit, you started to see how functions are useful in writing animations: functions help produce information (such as the height of a rocket) that are used to create scenes in a game. With the extra practice writing functions and using the Design Recipe, you are now ready to start writing functions that will move the elements within your own game.

                        imageBootstrap by Emmanuel Schanzer is licensed under a Creative Commons 3.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available at schanzer@BootstrapWorld.org.