Haskell For Kids: Week 1!
And We’re Off!
Thanks again to everyone that’s supported this project and stepped up to be a part of it. Today, I taught my first in-person class on Haskell, and it was a blast! This is my first weekly summary post, containing what we’re doing this week.
Since there are a number of kids following along with this, let’s all get started with some introductions!
- Me: My name is Chris Smith, and I’m teaching the in-person programming class at Little School on Vermijo that got all of this started. I’m a huge fan of Haskell, and am really excited to be able to share that with new people!
- Sue: Sue Spengler is the “founder, lead teacher, principal, superindendent, custodian, secretary, and lunch lady” for the Little School on Vermijo. The school is her project, and she’s doing some pretty amazing things. I had to poke around a bit for a photo, so I hope she likes this one!
- My local students: The kids in my class today were Grant, Sophia, Marcello, and Evie (I hope I spelled that right!) I’ll ask them to introduce themselves in comments on this post, so look for them there!
- Everyone else: Any other kids who are taking the class, please use the comments to introduce yourselves as well! You can say hello, and if you like, you can even link to a video or picture.
I hope everyone takes the time to leave comments and say hello to each other. Learning things is a lot more fun when you talk to other people.
We talked about where we’re going, including:
- Write computer programs to draw pictures.
- Change our computer programs so the pictures move!
- Build a game of your own choosing.
This will take the school year! That’s because this class isn’t just about memorizing some thing about a particular computer program: it’s about being creative, trying things, and doing something you’re proud of. So there will be a lot of free time to play around and try out different ideas in your programs. We are learning the Haskell programming language, but in the end, the class is more about being in control of your computer and designing and building something really cool from scratch, not just remembering some stuff about Haskell.
Organization of Computers and Programming
The first thing we talked about was what a computer program is, and how some of the ideas fit together. Here’s the whiteboard when we were done!
Some of the ideas we talked about:
- How a computer works. The main part of a computer is built from a device for following instructions (the “CPU”), and a device for remembering information (“memory”).
- Machine language. The computer doesn’t speak English, of course! It follows instructions in a language called “machine language”. This language is easy for the computer to understand, but it’s very, very difficult to write programs in.
- Compilers. Instead of writing our programs in machine language, we write them in other languages, and then get the computer to translate them for us! The program that does that is called a compiler.
- Libraries. Libraries are pieces of programs that other people have written for us, so we don’t have to start from scratch. We spent some time imagining all of the steps involved what we might consider very easy things to do with a computer. For example, thing of all the little steps in drawing a window… how many circles, rectangles, lines, letters, and so on can you find in a window on your computer? Libraries let someone describe things once instead of making you repeat all that each time.
We talked about how we’ll be using:
- A programming language called Haskell.
- A library called Gloss.
At this point, we all used a web site to write some simple computer programs using Haskell and Gloss. The web site is:
We started out with some really simple programs, like these:
Draw a circle!
picture = circle 80
Draw a rectangle!
picture = rectangleSolid 200 300
Draw some words!
picture = text "Hello"
All of these programs have some things in common:
- The first line of each one is “import Graphics.Gloss”. This tells the compiler that you want to use the Gloss library to make pictures. You only need to say it once, and it has to be at the very beginning of your program.
- They all then go on to say “picture = …”. That’s because the way our programs work is to make a picture, and call it “picture”. The web site we’re using then takes that picture, whatever we define it to be, and draws it for us. We talked about how in the future, we might define other things with other names, but for now, we’re okay with just telling the compiler what “picture” is.
- After the “=”, they describe the picture that we want to draw. There are several types of pictures, and we’ve just seen three of them! All of the kinds of pictures you can create are part of the Gloss library.
- Except for the last one, they all use some distances. For example, the 80 in the first example is the radius of the circle (the distance from the middle to the outside). You can make that number larger to draw a bigger circle, and smaller to draw a smaller circle. You can do the same with the width and height of the rectangle.
We did have problems with some people using the web site. If you’re having trouble, you might need to make sure you have a new version of your web browser. Also, the web site doesn’t work with Internet Explorer… so try with Chrome, Firefox, Opera, or Safari. Don’t worry too much about the web browser problems: soon enough, you’ll install the Haskell compiler on your own computer, and you won’t need the web site to run your programs any more! We’re just using the web site to get started quickly.
Drawing more than one thing!
By this time, several kids were asking if they can draw more than one shape at a time. Yes, you can! To draw more than one thing, you can use “pictures” (notice the s at the end). For example,
picture = pictures [ circle 80, rectangleWire 200 100 ]
Notice we do this:
- The word “pictures”
- An opening square bracket.
- A list of the pictures we want to draw, separated by commas.
- A closing square bracket.
We talked about how it helps to make new lines in your program sometimes. The only think you need to be careful of is that when you make a new line, you have to put a few spaces at the beginning to indent it. See how the second and third lines of the part where we define “picture” are indented a little?
Changing your pictures
The Gloss library gives you some ways you can change your pictures, too!
You can change the colors with “color”.
picture = color red (circleSolid 80)
Notice how you say “color”, then the name of the color you want, and then the picture to draw, in parentheses. The parentheses are important! They mean the same thing they do in math: treat that whole part as a single “thing” (in this case, a picture).
We talked about what colors Gloss knows about. Here’s the list: black, white, red, green, blue, yellow, magenta, cyan, rose, orange, chartreuse, aquamarine, azure, and violet. We all laughed because Sue picked a weird color name off the top of her head, and asked “Does it know chartreuse?” Yes, it does. Lucky guess!
You can also move things around on the screen.
picture = translate 100 50 (rectangleSolid 50 50)
When you want to move things around, Gloss calls that “translate”. Yes, it’s a weird name, but “translate” just means move something left, right, up, or down. The first number after translate is how far to move it to the side. Positive numbers mean right, negative numbers mean left, just like a number line! The second number is how far to move it up or down. Positive numbers mean up, and negative numbers mean down.
Keep in mind that in Haskell, you have to write negative numbers in parentheses! If you say “translate -100 …”, then Haskell thinks you want to subtract one hundred from “translate”. It doesn’t know how to subtract a number from a verb (I don’t either) so it gives up! You have to write “translate (-100) …” instead, with the parentheses.
You can also turn things. The verb for that is “rotate”. Let’s draw a diamond.
picture = rotate 45 (rectangleWire 100 100)
You rotate things in degrees. Remember that 360 degrees means turn it all the way around to where it started, so it won’t do anything! 45 degrees is half of a right angle. Do you see why that gives you a diamond?
The last thing you can do is stretch the picture. The verb for that is “scale”.
picture = scale 2 1 (circle 100)
That will draw an ellipse, which is like a circle but it’s wider than it is tall!
Don’t worry if this all doesn’t make sense yet! We’ll be spending a lot of time playing around with how to put these things together! Here’s the whiteboard after we finished all of this…
Time for Experimentation
We spent a lot of time with everyone making their own shapes and pictures of whatever they want. The best way to get more comfortable with all of this is to play around. Make lots of little changes and see what happens! Try to guess what’s going to happen, then try it and see if you’re right or wrong.
Here are some pictures of the kids with their projects:
Sophia and Evie showing off two circles side by side. These eventually became the eyes in a face!
That’s Grant with his diamond. It looked even better after he stretched it a little bit up and down.
This was Marcello’s graphics… centering the word in the circle was a long task! If you try it, you’ll notice text doesn’t get normally drawn right in the middle like other pictures do, so Marcello put in a lot of trial and error time with “translate” to put the word in the circle.
That’s Sophia being very excited at getting her two eyes in the right places!
Your mission, if you choose to accept it… is to plan and create a drawing of something you’re interested in! Maybe it’s a fish, a garden, a space station, or a dragon… just make sure you can draw it by using rectangles and circles of different colors, and moving, turning, or stretching them. Here at the Little School, we’ll be spending our remaining class this week and our classes next week working on this. Spend some time and come up with something you’re proud of!