Wednesday, May 20, 2009

Teaching Functional Programming To Kids

I started teaching some of the basic concepts of functional programming to my 8 year old son yesterday, and wanted to write a little about it. The wonderful thing about it is that kids really are ready to learn the concepts at a very young age. I'm not actually teaching him programming, just concepts, but when the time comes, he'll basically already know how to program.

I have what I think is an absolutely perfect example. It's one that all parents and kids can identify with: The Dr. Seuss Star Belly Sneetch machine.



This is a simple machine that takes in a Sneetch without a star on its belly, and spits out a Sneetch with a star on its belly. I'm just going by memory here to say that I think kids can probably understand this concept at the age of 3. In this post I'm going to use this style to represent machines:

-----------
| |
Sneetch ---> | * | ---> Star Bellied Sneetch
| |
-----------

This is the same as the actual picture above, but works for all cases since I don't have pictures for all the concepts I want to represent.

In the Dr. Seuss book there is also the opposite machine that removes the star from a star bellied Sneetch.

-----------
Star Bellied | |
Sneetch -> | -* | ---> Sneetch
| |
-----------

While that seems really simple, it's all we need to start teaching a wide range of concepts to kids. I started with this one, because of its similarity with the machines above (for all the things below, Jacy and I worked them out on a whiteboard. But, paper is just as good):

-----------
| |
10 ---> | +5 | ---> 15
| |
-----------

Here we have a machine that adds five to whatever you put into it. Very simple, very easy for kids to understand. It helps to run a few more inputs through (0, 10, a billion) just to let them know that the box doesn't just take 10 and give 15, it works with all numbers.

After this one I followed up with another very easy one.

-----------
| |
10 ---> | -5 | ---> 5
| |
-----------

At this point he pointed out, "Well it could be a divided by two machine instead." This was unexpected, and impressive, and at some point I'll talk about it further...but not yet. It was great to feel that he was understanding it though.

Now that he was getting it, it was time to change things up just a little bit. I introduced the plus and minus machines, which take two inputs instead of one.

-----------
7 ---> | |
| + | ---> 19
12 ---> | |
-----------

-----------
12 ---> | |
| - | ---> 5
7 ---> | |
-----------

These presented no challenge whatsoever. In fact (I guess rather surprisingly), nothing I taught him presented any sort of challenge. Next I introduced the biggest and smallest machines (which we programmers call max and min).

-----------
7 ---> | |
| Biggest | ---> 12
12 ---> | |
-----------

-----------
7 ---> | |
| Smallest | ---> 7
12 ---> | |
-----------

-----------
10 ---> | |
| Smallest | ---> 10
10 ---> | |
-----------

I guess he was a bit surprised when I showed him the last one. But, it only took showing him the answer once for him to fully understand.

I then added an equals machine that spits out YES! if the two numbers are equal, and NO! if they aren't (true and false, obviously). This is different because now we were no longer working with numbers as the inputs and outputs.

-----------
7 ---> | |
| = | ---> NO!
12 ---> | |
-----------

-----------
7 ---> | |
| = | ---> YES!
7 ---> | |
-----------

Simple, but effective.

Now, Jacy and I have done considerable work with logic gates, and I wanted to show him how logic gates are really just like machines. I also taught him the word Function at this point, but didn't push it. Kids can relate to machines, not functions.

------------
ON ---> | |
| AND GATE | ---> OFF
OFF ---> | |
------------

------------
ON ---> | |
| OR GATE | ---> ON
OFF ---> | |
------------

------------
ON ---> | |
| AND GATE | ---> ON
ON ---> | |
------------

While the logic gate examples seem simple, it tied two worlds that we've been working on together very nicely.

Fun


My son has a really short attention span, and all the while I'm doing this I have to think of different ways to make it fun. If it's not fun, hes just going to go play video games. Rightfully so, video games are fun. There were a few ideas I tinkered with before settling on the Dr. Seuss machine. One was a monster that stuffs stuff into his mouth and then spits out the answer. I thought that one was kind of neat. The point is, if you plan on teaching your child, think of something fun they can relate to.

Combining Machines



I could sense we needed some more fun at this point, and we'd learned enough basic machines that I thought it would be great to start combining them. After trying this, I recommend starting with all alike boxes. We did something a little more complicated and I ended up going too fast, and fell back on this:

-----------
7 --> | | -------
| + | --> 19 --> | |
12 --> | | | |
----------- | |
| + | --> 39
----------- | |
10 --> | | | |
| + | --> 20 --> | |
10 --> | | -------
-----------

After you get this first larger machine done, its pretty easy to add in more complicated machines. However, it might be good to wait until the next day, as Jacy was definitely getting it, but might have been getting a little fried. Here's an example though:

-----------
7 --> | | -------
| + | --> 19 --> | |
12 --> | | | |
----------- | |
| = | --> NO!
----------- | |
10 --> | | | |
| + | --> 20 --> | |
10 --> | | -------
-----------

And obviously, change the 7 to an 8 and get a YES! Different kinds of boxes doing and spitting out different kinds of things. In essence, this is really all we do as programmers.

Types


Being a lover of static type systems, I also talked to him about types, by saying "kind(s) of things". For example, I asked him, "What kind of thing does this machine take in (or what kind of thing do you put into this machine)?"

-----------
| |
10 ---> | +5 | ---> 15
| |
-----------

Answer: Number. I avoided Integer for now. What kind of thing does it spit out? Answer: Number.

I then showed him this next example, which should arguably have a section of its own:

-----------
| |
D ---> | +5 | ---> I
| |
-----------

This machine looks exactly the same as the machine above, except you put Letters into it, and it spits out letters. We also did months. Both are interesting because they have to loop around. I didn't have to teach him that, he just got it.

Then I introduced a formal notation for types:

-----------
7 ---> | |
| + | ---> 19
12 ---> | |
-----------

(Number,Number) -> Number

And introduced machines that change the type (he had seen it already, but only with YES! and NO! This, I think, is a better example):

-----------
| |
5 ---> | To Letter | ---> E
| |
-----------

Number -> Letter


He understands the notation and can write it if I give him slots to fill in like this:

______ -> ______

or

(______, ______) -> _______


Things I don't know how to teach, Yet


I certainly didn't try to teach him anything about machines that take in machines and spit out machines. Also, some of my boxes were polymorphic, but I don't think I know how to explain that to him.

For now, I think Jacy and I will just do this same stuff for a while, reinforcing it. I'm not sure what the best thing to teach him next is. Some of the stuff here I've skimped on writing up, and we actually spent more time on than it seems.

Anyway, this was all really, really fun, for both of us.

Goose flies upside down

This is the moment a goose was caught performing an extraordinary upside-down contortion as it battled to land in heavy winds.
This greylag goose was captured by wildlife photographer Brian McFarlane in a manoeuvre known as whiffling.The bird was captured by a wildlife photographer flying with its neck twisted 180 degrees and its body seemingly facing the wrong way.The manoeuvre may look painful but it is a known tried and tested way of braking, called whiffling.In amazing twists and turns, birds spill air out of their wings and can slow down rapidly and reduce height. The results, however, are not usually this extreme.This bird, a greylag goose, was photographed as it came in to land on a freshwater lake at an RSPB reserve in Strumpshaw, Norfolk. It was captured by wildlife photographer Brian McFarlane who said he couldn’t believe his eyes.
“I cannot believe how this goose got into such an incredible position. It was a windy day and it was making life difficult for the birds. Some were more expert at controlling their flight while others were tossed around in mid-air.”
Paul Stancliffe, of the British Trust of Ornithology said he had seen the extraordinary move many times in 36 years of bird watching. “I have, however, never seen a photograph of a bird in mid-whiffle like this. It is an amazing photograph.”
Mr Stancliffe said whiffling makes the bird “drop like a stone so that they instantly lose height”.
“They look like they are out of control but they are actually very much in control,” he added.






Tuesday, May 19, 2009

Dream Car Stinger Car Of The Future

Some people have always wanted to know what cars might look like in the future so here it is Dream Car Stinger

Saturday, May 16, 2009

Awesome Swine Flu Masks In Maxico

Mexico Has Awesome Swine Flu Masks
Just when you thought everything was going to hell in Mexico, they turn around and come out with this awesome swine flu mask. Lookin' good, Mexico.




We're just waiting for swine flu masks that look more like this...

Swine Flu Ads From 1976

Check Out These Swine Flu Ads From 1976
This swine flu thing seems to be all everybody is talking about today. Well, check out these old ads about swine flu from 1976. And just for fun,




here's a picture that used to be cute, that's now terrifying...


Greatest Swine Flu Headline Ever

Greatest Swine Flu Headline Ever We would have lost that bet. Afghanistan has one pig. And it's in a zoo. That zoo sounds amazing, by the way. Obviously Muslims don't eat pigs and whatnot, but one pig? And they quarantined it?


The best part is in the article where they refer to that pig as Afghanistan's "only known" pig. The rest are hiding in caves on the Afghan/Pakistani border.

Sunday, May 3, 2009

Artist creates invisible car

Art student Sara Watson has found the ultimate way of avoiding traffic wardens by making her car invisible. The 22-year-old student at the University of Central Lancashire spray painted a battered Skoda Fabia to match the car park and entrance to her art studio.
Her work, created as part of her drawing and image making course at the university, creates the illusion that the car is see through. She was given the car from a breakers yard and worked for three weeks to ensure that it blended perfectly with its surroundings.
"I was experimenting with the whole concept of illusion but needed something a bit more physical to make a real impact." said Miss Watson, who is from Ashton under Lyne.
"People have been stopping in the street to look and coming up and almost bumping into it, so it's had the desired effect."



The car is reminiscent of the work by pavement artist Julian Beever, whose attempts to trick people's minds into seeing perspective on the flat surfaces of paving stones.Steve Jackson, owner of Recycling Lives, the firm that gave Miss Watson the car, said: "When I first saw the photos I was convinced it was something which had been done on the computer, but when you look more closely you see the effort and attention to detail she has put into it. It is just amazing."The car is now being used for advertising by the local recycling firm that donated the vehicle.