# Haskell For Kids: Week 8

Welcome to the Week 8 summary of my Haskell class for kids. If you’re just joining us, feel free to browse through weeks 1, 2, 3, 4, 5, 6, and 7.

# A Brief Review

Just to set the stage: we spent quite a few weeks on pictures, and just last week we started on animations. We learned:

- Whereas pictures were just
*variables*, animations are*functions*. - They have a single
*parameter*, which is how many seconds since the program started running. - The
*result*of the function should be a picture.

To define a function, we give the parameter a name on the left side of the equal sign, right after the function name. We can then use that name after the equal sign, just like if it were a number. Here’s an example:

animation t = rotate (60*t) (rectangleSolid 40 200)

The function is called *animation*. The parameter is called *t*. And after the equal sign, we can use *t* just like any other number. For example, we can multiply it by 60, and then rotate the picture by that many degrees.

The result of functions comes from *substitution*. So to find out what this animation will look like after 5 seconds, the computer just looks at everything after the equal sign, and replaces any *t* that it sees with a 5. So it takes the following steps:

*animation 5*– this is what the computer starts out trying to figure out.*rotate (60*5) (rectangleSolid 40 200)*– It replaces any*t*by 5 after the equal sign.*rotate 300 (rectangleSolid 40 200)*– This is just math: the computer calculated 60 times 5, and got 300.

So after 5 seconds, the computer will draw the rectangle rotated by 300 degrees. If you try this out with a few numbers, you’ll notice that the rectangle turns by more and more degrees over time, making it spin.

### So What’s the Problem?

We played around with animations that change lots of things: by *t* in different places, you can make things spin (using *rotate*), move (using *translate*), or get bigger or smaller (using *scale*, or by using *t* as the parameter to Gloss functions like *circle*). But you might have noticed something disappointing about doing anything except rotation: after a while, the numbers get too big to see! Things move off the edge of the screen, or get so big you can’t see them any more.

Because of this, our programs so far haven’t really done anything except rotate. To do much more without everything ending up too big or too far off the screen, we’re going to have to get comfortable with a different kind of function…

# Functions That Give Back Numbers

Animations are functions that get a number, and turn it into a picture. But that’s not the only kind of function! We’ve actually seen quite a few kinds of functions, if you count the ones that Gloss defines for us:

*circle*is a function that turns a number into a picture (just like animations, but the number means something different)*light*is a function that turns a color into a different color.*rotate*is a function that turns a number**and**a picture into different picture (one that’s rotated).*polygon*is a function that turns a**list**of points into a picture.

In general, you can think of a function as a machine that turns things into other things:

You feed the parameters in, and you get the result out the other side. Different functions need different types and different numbers of parameters, and they give you different types of results, too. So when you want to use a function, the first things you should ask are: (1) How many parameters does it need? (2) What types of things does it want for its parameters? And, (3) What type of thing is it going to give me back when it’s done?

In the end, you want to create pictures, of course! So at first you might think that you’re only interested in functions that give you pictures back. But that would be short-sighted! Remember the functions *light* and *dark*? They are functions that expect one parameter – a color – and give you back another color, *not* a picture. But they’ve still been very useful for building your pictures, haven’t they?

The same thing is true with numbers. Even though you ultimately want to end up with a picture, it’s very useful to be familiar with some functions that work with numbers, because we *use* numbers all the time when we’re making pictures. So we’ll spend a lot of this week talking about functions that work with numbers.

## How to Draw a Function on Numbers

When you have a function that expects a number for its only parameter, and gives you back a number as a result, we have a convenient way to draw it. It might look weird at first, but believe me, it makes things a lot easier! So here’s how to draw a function. It only works if the function is from numbers to other numbers.

We start out by drawing two lines: one horizontal, and one vertical, like this:

I’ve called the vertical line *f* and the horizontal line *t*. That’s because normally, the horizontal line will represent the amount of time the program has been running, and we’ve been calling that *t*. The *f* is just short for “function”.

Next, you want to label the tick marks on the lines: for *t*, just number them 1, 2, 3, 4, and so on. Zero is where the two lines cross, so the first tick mark after that should be 1. For the vertical line, how you label it depends on the numbers you’re working with! Notice it’s got both positive and negative numbers, too.

The next step is to draw some points. Ask yourself, if you give your function is given the number 0, what number does it give back? Now draw a point *directly* above or below the point on the horizontal line that means zero (remember, that’s the point where the lines cross). How far up or down you draw the line is decided by what the function gives you back. Now do the same thing for 1, and for 2, and so on…

Finally, once you’ve got a bunch of points, connect them with a smooth line. That line is how you draw that function.

### Example

Let’s try an example. We’re going to draw the function

f t = t/2 - 2

That means the function is called *f*, its parameter (the number you give it) is called *t*, and the number it gives you back is half of *t*, minus 2.

Let’s draw the lines like we did before, and label the points counting by one in both ways:

The next step is to figure out some values of the function, and draw them as points on the graph:

f t = t/2 - 2

f 0 = 0/2 - 2 = 0.0 - 2 = -2.0 f 1 = 1/2 - 2 = 0.5 - 2 = -1.5 f 2 = 2/2 - 2 = 1.0 - 2 = -1.0 f 3 = 3/2 - 2 = 1.5 - 2 = -0.5 f 4 = 4/2 - 2 = 2.0 - 2 = 0.0 f 5 = 5/2 - 2 = 2.5 - 2 = 0.5 f 6 = 6/2 - 2 = 3.0 - 2 = 1.0 f 7 = 7/2 - 2 = 3.5 - 2 = 1.5 f 8 = 8/2 - 2 = 4.0 - 2 = 2.0 f 9 = 9/2 - 2 = 4.5 - 2 = 2.5 f 10 = 10/2 - 2 = 5.0 - 2 = 3.0 f 11 = 11/2 - 2 = 5.5 - 2 = 3.5 f 12 = 12/2 - 2 = 6.0 - 2 = 4.0 f 13 = 13/2 - 2 = 6.5 - 2 = 4.5

Here’s the graph with the line that connects those points:

### How to Read the Drawing

Looking at the graph, you can get a good feeling for what that function does. Imagine you’re always moving to bigger and bigger numbers of seconds since the program started (because you are!). Then this function will keep getting bigger and bigger forever. You can also see that it will keep getting bigger at the same speed. And you can see that it starts out being -2, and then after 4 seconds it’s 0, and it keeps going up from there.

So what does that mean?

- If you say
*rotate (t/2-2)*, that means start out rotated a little bit clockwise, and then turn counter-clockwise at a constant speed forever. - If you say
*translate (t/2-2) 0*, that means start out a little to the left, then move right at a constant speed forever. - and so on…

## Practice Drawing Functions on Numbers

In class on Tuesday, we did this worksheet together, where we practiced drawing functions that use numbers.

## Some Useful Functions on Numbers

You might notice at the end of the worksheet a few functions that we haven’t used yet that are useful:

*min*and*max*. These functions actually take two numbers as parameters, and they return one of the two numbers you give them. So*min*gives you the smaller of the two numbers, and*max*gives you the larger of the two numbers.*sin*. This function, which is pronounced like “sign”, is useful for making things move back and forth. It is way more interesting than just that, but we’ll save the more interesting stuff for high school trigonometry classes. All we want it for is to make things move back and forth. Keep in mind that*sin*itself only goes back and forth between -1 and 1, so you probably want to multiple the answer by something bigger!

Want to see what the sin function looks like? Here you go:

See how graphs of functions help you understand what they do?

# Your Task

Because the Little School is off next week for a inter-term break, it will be two weeks until my next summary! Never fear, though, I’ve got something for you to work on.

Your task is to make an animation of an amusement park ride. Try to get several kinds of movement in there: rotating in circles, moving back and forth, and anything else you can do! Those of us in my class are going to make these:

- Skyler: Roller coaster
- Grant: Bungee Jump Ride
- Marcello: Ferris Wheel
- Sophia: Tilt-a-Whirl
- Sue: Swinging Pirate Ship
- Me: Kids Jumping Rope (the example from week 7, but I might tweak it a little)

We’re planning to put them together into a gigantic amusement park picture when we’re done. If you have friends doing this at the same time, you can, too! (Do you know how? It’s just like top-down design: get everyone to send you their programs, but call them something other than *animation*, then scale them, move them to different places, and combine them using *pictures*.)

Good luck! And as always, use the comments to ask for help if you’re stuck.

This is very cool. My earliest programming experience was with Logo (http://el.media.mit.edu/logo-foundation/logo/programming.html). I think graphical programming is good as a first experience because you can make mistakes and still get interesting results.

I was wondering if you had planned more online lessons.

I feel sad.

Just wondering if this is the last of the “Haskell for kids” posts…

Hello, colleague.

Please, tell me, is that really the last post on this great theme? It’s very interesting for me just because I translate this serie to Russian. If you are interested, you can find the drafts here: https://docs.google.com/document/d/1CgPbpaUp1ehazF1o_chTOJJ2Pvf4PucIsERhJQon6tQ/edit

Yes, this is the last instance of the blog posts. I am still teaching the class, but I found it difficult to make the right decisions for my students when I was also concerned with making sure I had covered enough interesting material for a blog post.

I am, though, working on putting together a more concrete curriculum with worksheets, activities, and projects, a timeline, alignment with educational standards, etc. When I have something to announce there, I will say so.

Thank you for information. I hope you will find the time smewhere in the future to finish your posts :).

Come back, please :)

This is not only for kids. We also want to have fun with Haskell :)))