P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object...

34
Page | 1

Transcript of P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object...

Page 1: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 1

Page 2: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 2

Table of Contents

Lesson 0: Teacher Overview ..................................................................................................................................... 3

Lesson 1: Basic Functions ............................................................................................................................................ 8

Lesson 2: Type Safety ................................................................................................................................................. 13

Lesson 3: Side Effects ................................................................................................................................................. 18

Lesson 4: Putting It All Together ........................................................................................................................... 21

Page 3: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 3

Page 4: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 4

Teacher Overview Hello teachers and parents! Thank you so much for your interest in

using our curriculum guide to teach your second graders about

functional programming.

Introduction

For starters, you are making a huge impact by opening this book. We aim to inspire your

second graders to be the next innovators of the world by teaching them a new way of

approaching problems. Functional programming is important because it is the future

of programming due to its security and applications with parallel and cloud

computing. It teaches students to break down problems into smaller components that

perform specifically what is required of them.

If you are not familiar with programming or functional programming, don’t worry. You

can always learn along with the students. We also have a list of resources listed at the

end of this lesson that you can use for an in-depth understanding of these concepts.

Before you use this curriculum, here are in brief, the concepts covered in the lessons.

Functions

In programming, a function is essentially an object or operation that, given an input,

always produces the same output. A good example of this would be a mathematical

operation. For example, a multiplication function takes in two numbers and returns

their product. A division function takes in two numbers and returns their quotient.

We can also consider objects to be functions as well, if they have an input and an output,

as long as the same input always has the same output. For example (an example used

extensively in this curriculum guide), a caterpillar can be considered a function if it

takes in red food as an input and returns poop of the same color as the output.

Page 5: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 5

Type Safety

Type safety is when you control the types of inputs your function

takes in, so that unwanted results do not happen. For example,

using the division operation as a function, we want to make sure

that our function only takes in real numbers. We also want to

make sure that the second input is not a 0, so that division is

possible (since division by 0 is impossible).

By restricting the types of numbers our function can take in, we protect our function

from failing to perform the task or having unexpected results.

Side Effects

To prevent unexpected things from happening, we always want our function to do only

whatever gives us an output and not impact anything else. When a function changes

the state of some other information or interacts with another function, it causes a side

effect. One example of a side effect would be having a function return an output and

print something to the screen (since we are changing the state of the screen).

A function that avoids side effects is a pure function. Avoiding side effects while

programming is useful since it prevents unexpected behavior of a program.

Putting It All Together

The last lesson is a game that brings all of these ideas together.

Functional Programming

Functional programming is a programming methodology where a problem is broken

down into functions without side effects. Unlike object oriented programming, we

avoid assigning mutable states to objects in functional programming. This approach is

useful for parallel computing, since multiple functions can run independently without

having unexpected errors due to changing states of the data. Functional

Page 6: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 6

programming is also useful in terms of safety since unexpected

behavior of functions are avoided and states are not assigned.

How to Use This Curriculum

Each of the concepts mentioned has its own lesson plan which can

be integrated into English and Math classes and can be covered

in one class or several classes. These lessons consist of learning

objectives, simple explanations of the concepts (using strategies like repetition, visual

metaphors, and examples to facilitate better learning), and fun activities for children

to use what they learned.

Each lesson has an activity involving a caterpillar metaphor that ties in with its prior

lessons. Additional activities have been included to broaden the understanding of

these concepts. Most of the activities in this book are group activities to encourage a

discussion based approach to the material.

Our curriculum guide also uses the following tools:

Discussion questions- Whenever you see this dialog box, be sure to ask your class this

question and see what everyone thinks. These are questions to encourage

classroom discussions and provide food for thought while teaching the material.

Definitions- Be sure to keep these in mind while teaching! These are simplified

definitions to use for the concepts mentioned in the book. Be sure to explain and

reiterate this definition so students can better understand it. All key words are bolded

throughout the lessons.

Alerts- Whenever you see this box, these are points to be conscious of while teaching.

There are used to emphasize the importance of a step in the curriculum or provide

flags for potential questions or concerns in a lesson.

Now that we’ve covered the basics of our curriculum guide, I hope it helps you as you

point the next generation of technologists in new directions!

Discussion

Question

Alert

Page 7: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 7

Sources

http://everybodyisageniusblog.blogspot.com/2012/12/teaching-

functions.html

https://code.org/curriculum/algebra/courseA/1/index.html

http://snap.berkeley.edu/

Page 8: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 8

Page 9: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 9

Basic Functions Welcome to the first lesson! In this lesson, students will learn the definitions and ideas

behind input, output, and functions.

Introduction

Start by telling the students that they will be learning some new words: input, output,

and functions.

Input- anything you put IN to an object

Output- anything you get OUT of an object

Function- an object that takes in some inputs and produces one unique output

Caterpillar Activity

Start by reading The Very Hungry Caterpillar aloud or watch the video.

Explain that they will be using this story to learn more about inputs, outputs, and

functions.

Have a discussion based on the following questions:

• “What do you think happens after the caterpillar eats food?”

o Guide their answers, comparing a caterpillar to themselves.

• “What happens when we eat?”

o They should mention poop!

• “In the wild, why is poop important?”

o If they don’t know, tell them about how poop is useful for providing

nutrients to plants. Poop can be used to fertilize the soil and help plants

grow.

• “Have you ever seen worms while you’re digging?”

o Worms break down things they eat and produce poop. Their poop makes

the soil healthy. So when you see worms while you’re digging, that’s a

good thing!

Page 10: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 10

Have them consider the caterpillar and recall what an input is. Input is anything you PUT

IN to an object. In this case, the caterpillar is our object. We are PUTting food IN to

the caterpillar when it eats. In this case, food is the input.

“What are some other examples of inputs that you can think of?”

Recall what an output is. Output is anything you get OUT of an object. In this case, the

output of the caterpillar is its poop. Usually, we want outputs that are useful to other

objects. What was the poop of the caterpillar useful for?

“What are some other examples of output?”

Imagine that the caterpillar is now eating magic food. This magic food makes the

caterpillar poop out the same color that it ate. Create a discussion around the

following questions and talking points:

• “What is our input now?”

o Colored food

• “What is our output?”

o Colored poop- the same color as the food

Print out or write this on a board to have students match inputs with outputs.

Discussion

Question

Discussion

Question

Page 11: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 11

Now that they are comfortable with inputs and outputs, ask them if they remember what

the definition for a function is. A function is an object that takes in an input and

always gives out the same output for that input.

Follow the questions and talking points below to help answer the question “Is our

caterpillar considered a function?” Remind them that the input is the colored food,

and the output is the colored poop.

• “Does each of our inputs always give us the same output?”

• “Does a red apple ever give us the same output?”

• “Is it okay that both strawberries and apples make our caterpillar poop red?”

o Yes, because functions only care that the output is the same for whatever

you put in.

If you have a student ask about rainbow food, tell them that this will be covered in the

next lesson about type safety. The caterpillars in this lesson only eat food that is one

color.

“What are some other functions you can think of?” “What are some other things that

when you put something in, you always get the same thing out?” This can be a

discussion for groups in larger classrooms. End the discussion by going over

examples that each group came up with. For each example, make sure they can

answer what the input is, what the output is, and if the same input always gives

them the same output.

Extra Activity

Read The Sneetches by Dr. Seuss aloud or watch the video.

Start a discussion with the following questions:

• “What were the two different machines that Sylvester McMonkey McBean

brought to the Sneetches?”

o Star On and Star Off Machines

• “What happens when you put a plain belly sneetch in the Star On Machine?”

Alert

Discussion

Questions

Page 12: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 12

• “What happens when you put a star belly sneetch in the Star Off Machine?”

Either verbally explain the following, or show the students the following “special

machine” and ask the following questions:

• “What is this machine doing? What is the pattern?”

• “What if our machine did this?”

o This tells us that our machine is broken. We should’ve gotten 2 points for

1 banana.

o Note that it’s okay that two objects can return 2 points. 1 banana and 1

computer both give us 2 points. The important thing is that one banana

always gives us 2 points, and 1 computer always gives us only 1 point

each.

Can you find more machines, that given an object, always return the same thing? Have

them think about light switches, pressing the popcorn button on a microwave, or

opening presents.

Ask them if they remember what these kinds of machines are called- functions!

Discussion

Questions

Page 13: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 13

se

Page 14: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 14

Type Safety In this lesson, students will learn to be careful about what they are putting in to their

functions, or type safety.

Introduction

Start by telling them that when we have functions, we must be careful about what we put

in to them, since certain inputs could give us outputs that we don’t want. Bad inputs

could even break our functions! Ask a few questions:

• “When we put bread in a toaster, what is our output?”

• “Would you ever put eggs in a toaster!”

o Hopefully, this ridiculous example will show them that certain inputs make

no sense.

Tell them that this is what type safety is all about. Type safety means we care about

being safe with the type of inputs that we put in to our functions.

Caterpillar Activity

Start by asking some questions:

• “What does it mean to be allergic to certain foods?”

Have students imagine that they have a red caterpillar that is allergic to certain foods.

Normally, if a caterpillar eats green food, it will just poop green. Our red caterpillar is

allergic to any food that is not red. If it were to eat a green leaf, the caterpillar would

get sick and its poop will be an unexpected brown color. A safe caterpillar would

refuse to eat any type of food it is allergic to. For example, if you give a safe, red

caterpillar a green leaf, it will just say no, and it wouldn’t get sick!

Page 15: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 15

Use the following to create a coloring worksheet. Tell them to pick a color for their

caterpillar and color it.

Make sure the caterpillar is all one color!

Have them circle the foods that their caterpillar can eat to be a safe caterpillar. Make

sure the food they circle is the same color as their caterpillar. Ask them to draw what

the caterpillar would look like if it was not safe and ate any of the food that it is

allergic to.

Alert

Page 16: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 16

End the activity by having each student talk about their individual caterpillars.

Discuss with the class whether the caterpillar is a safe caterpillar and what types of food

the caterpillar can eat.

Extra Activity

This activity is based on chapter 7 of The Little Prince. There is also a film adaptation of

the book available on Netflix. Read the chapter or watch this adaptation before

completing the activity.

Repeat the following story. The Little Prince lives on his planet (an asteroid B-612) with

his love, his Rose. The Little Prince wants a sheep to eat the weeds that may grow into

baobab trees. The sheep is a function which takes IN a plant and gives OUT a chewed-

up version of that plant. The Little Prince can then throw away the chewed-up plant. A

safe sheep should only eat a certain type of plant- weeds. If the sheep is not safe, he

might accidently eat the Rose.

Ask:

• “How do we prevent the sheep from eating the Rose?”

o We need a sheep that can tell the difference between the Prince’s Rose

and other weeds.

The following activity will be a game played with pairs of students.

Materials: Red tissue paper, green tissue paper, green pipe cleaners, and four cups per

set of students.

Set up: Have each pair create one rose and three weeds. The roses and weeds should

each fit fully inside a cup.

Rules: Pick one student to be the sheep first. The other student is the Prince and will hide

the weeds and rose in the cups- one in each cup. Then, they will shuffle the cups

Discussion

Questions

Page 17: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 17

around. The sheep has to keep track of which cup is which in order to be safe. When

the Prince is done shuffling, the sheep must pick which cups they want to turn over

and chew up. If they eat the Rose, then they are not being safe! If they uncover the

weeds correctly, the sheep gets a point. Otherwise, the Prince gets a point. Switch

roles.

Winning condition: Whoever gets to 5 points first, wins!

Page 18: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 18

le

Page 19: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 19

Side Effects In this lesson, students will be introduced to side effects of functions.

Introduction

Start by introducing the following situations. Tell them that when we have a function, we

want it to do exactly one thing- take some input, and give us one output. Nothing

more, nothing less. If something else does happen, we call this a side effect. A side

effect is something happening that we did not plan on.

For example, suppose we have a toaster function, like in Lesson 2. The toaster takes IN

bread, and gives OUT toast. We also have a different function- you. You take IN toast

and eat it. Just like our caterpillars, food turns into poop. Imagine that the bread stays

in the toaster for too long. Not only is the toast burnt, but the whole toaster catches

on fire! The toaster (a function) created burnt toast (a side effect). This is a side effect

because we wanted perfectly golden brown toast, not gross burnt toast.

Now, not only did our toaster give us burnt toast, but it is also now broken because it

caught on fire. Now no one in your family, including you, can eat toast, since the toast

is broken. This is another side effect, but this time it is impacting a different function-

you!

Caterpillar Activity

Remind them of the Hungry, Hungry Caterpillar story. Ask:

• What happened when the caterpillar ate too much?

o He got a stomach ache.

• How many of you have ever gotten stomach aches from eating too much? What

did that feel like?

Explain that sometimes, our functions do things that we did not originally plan for. We

wanted the caterpillar poop to provide nutrients to our plant, but instead, we just

Page 20: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 20

have a grumpy caterpillar with a stomach ache. This is not what we wanted to happen.

The side effect in this case is the stomach ache.

In this case, the caterpillar getting sick affects only our caterpillar (the function itself).

This is called a local side effect.

Can you think of any other local side effects?

Have them imagine that they have multiple caterpillars eating a bunch of food that their

parents bought from the grocery store. Imagine that one caterpillar eats all the food.

Now, there’s no food left for any of the other caterpillars! This makes the other

caterpillars very hungry and sick. When the expected behavior of one function impacts

other functions, we have a normal side effect. This side effect can stop other functions

from doing their jobs.

Can you think of any other normal side effects?

Explain that while we want all caterpillars to be able to generate poop whenever they

want or need to make sure that we get the expected output at the expected time, we

have to make sure that we know how our functions work. We don’t want a caterpillar

eating too much and getting a stomach ache or making the other caterpillars hungry

by eating up all their food.

The next section will be combining all the ideas from the past three lessons.

Discussion

Questions

Discussion

Questions

Alert

Page 21: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 21

sdf

Page 22: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 22

Putting It All Together The learning objective of this lesson is for students to understand how functions, type

safety, and side effects work in the context of a game.

Introduction

Throughout this course we have learnt about functions, type safety and side effects.

Start by asking the kids about the different concepts they have learnt in the previous

lessons and for examples of those concepts. Be sure to go over the following points:

• “Functions are objects that take in some inputs to give a unique output. What

are some examples?”

o We have also learned that we should only put certain types of inputs in

our functions to make them safe, that way our function will not behave in

unexpected ways when given an input that doesn’t make sense.

• “Side effects are those unwanted, unexpected way a function can behave when it

does things other than return an output. What are some examples?”

Caterpillar Game

Preparation:

At the end of this lesson is the game board and the pieces. You can print these out

separately in color. It is recommended that you mount the pieces on cardstock or

construction paper so that the color is not visible from the back.

Background:

Each player in the game is a caterpillar. The game board consists of 4 fields and the 40

cards are different foods. Each field has 5 squares of soil to fertilize. Each corner of the

game board has a caterpillar. A caterpillar can only eat food of the same color (type

safety) to make poop of that color. The poop is magical so that poop of a certain

color fertilizes plants to grow food of that color. The objective of the game is to

fertilize all your fields with poop of your color before anyone else does.

Page 23: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional

P a g e | 23

Game rules:

1. At the beginning of the game, assign each player a caterpillar and a field. Shuffle the

cards and deal each player 5 cards.

2. Create a pile of the remaining cards after dealing. This is the pick up pile. Also, make a

place for discarded cards.

3. The order of the players is assigned in a clockwise fashion based on where a player is

sitting. The first player is always red.

4. Each player has the following 5 options during a turn:

a. Eat- Take a card from your hand matching the color of your caterpillar and place

it in the discard pile. Then collect a poop token of your caterpillar’s color. You can

only eat food the color of your caterpillar.

b. Fertilize- If you have a poop token, place your color token in your caterpillar’s

field to fertilize one square of soil.

c. Trade- Select a player who you want to trade a card with. Both players select a

card from their hand and exchange the cards at the same time.

d. Guess- Select a player. Make a guess about a food card in their hand. If you

guess right, you get their card and don’t give them anything. You can

automatically fertilize seeds with it. You don’t necessarily have to ask someone

for a food of your color. If you guess wrong, your turn is over. If a player runs out

of food cards in their hand, they starve as a side effect and eliminated from the

game.

e. Draw- Draw a card from the pick up deck.

5. The game continues for a player as long as they have food cards in their hand.

6. The game ends when one player has fertilized all the soil squares in their field.

Page 24: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 25: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 26: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 27: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 28: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 29: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 30: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 31: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 32: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 33: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional
Page 34: P a g e | 1€¦ · Lesson 0: Teacher Overview ..... 3 Lesson 1: Basic Functions ... Unlike object oriented programming, we avoid assigning mutable states to objects in functional