## Conversations with a six-year-old on functional programming

My six-year-old son walked up to me yesterday. “What are you reading?”

At the time, I was reading part of Janis Voigtländer’s habilitation thesis. Unsure where to even start, I decided to just answer straightforwardly: “I’m reading a very long story about free theorems.”

He persisted. “What are free theorems?”

Never one to shrink from a pedagogical challenge, I thought for a moment, then began: “Do you know what a function is?” He didn’t. “A function is like a machine where you put something in one end and something comes out the other end. For example, maybe you put a number in, and the number that is one bigger comes out. So if you put in three, four comes out, or if you put in six, seven comes out.” This clearly made sense to him, so I continued, “The type of a function machine tells you what kinds of things you put in and what kinds of things come out. So maybe you put a number in and get a number out. Or maybe you put in a list of numbers and get a number out.” He interrupted excitedly, “Or maybe you could put words in??” “Yes, exactly! Maybe you can put words in and get words out. Or maybe there is a function machine where you put other function machines in and get function machines out!” He gasped in astonishment at the idea of putting function machines into function machines.

“So,” I concluded, “a free theorem is when you can say something that is always true about a function machine if you only know its type, but you don’t know anything about what it does on the inside.” This seemed a bit beyond him (and to be fair, free theorems are only interesting when polymorphism is involved which I definitely didn’t want to go into). But the whole conversation had given me a different idea.

“Hey, I have a good idea for a game,” I said. “It’s called the function machine game. I will think of a function machine. You tell me things to put into the function machine, and I will tell you what comes out. Then you have to guess what the function machine does.” He immediately liked this game and it has been a huge hit; he wants to play it all the time. We played it while driving to a party yesterday, and we played it this morning while I was in the shower. So far, he has correctly guessed:

• $\lambda x.\, x + 1$
• $\lambda x.\, x - 3$
• $\lambda x.\, 10x$
• $\lambda x.\, 2x$
• $\lambda x.\, 6$
• $\lambda w\!:\!\text{English noun}.\, \text{plural}(w)$
• $\lambda x.\, 10 \lfloor x/10 \rfloor$

I tried $\lambda x.\, \min(x+2,10)$ but that was a bit tough for him. I realized that in some cases he may understand intuitively what the function does but have trouble expressing it in words (this was also a problem with $\lambda x.\, 10 \lfloor x/10 \rfloor$), so we started using the obvious variant where once the guesser thinks they know what the function does, the players switch roles and the person who came up with function specifies some inputs in order to test whether the guesser is able to produce the correct outputs.

$\lambda x.\, 6$ was also surprisingly difficult for him to guess (though he did get it right eventually). I think he was just stuck on the idea of the function doing something arithmetical to the input, and was having trouble coming up with some sort of arithmetic procedure which would result in $6$ no matter what you put in! It simply hadn’t occurred to him that the machine might not care about the input. (Interestingly, many students in my functional programming class this semester were also confused by constant functions when we were learning about the lambda calculus; they really wanted to substitute the input somewhere and were upset/confused by the fact that the bound variable did not occur in the body at all!)

After a few rounds of guessing my functions, he wanted to come up with his own functions for me to guess (as I knew he would). Sometimes his functions are great and sometimes they don’t make sense (usually because his idea of what the function does changes over time, which of course he, in all sincerity, denies), but it’s fun either way. And after he finally understood $\lambda x. \min(x+2, 10)$, he came up with his own function which was something like

$\lambda x:\mathbb{N}. \begin{cases} 10 - x & x \leq 10 \\ 10 & \text{otherwise} \end{cases}$

inspired, I think, by his kindergarten class where they were learning about pairs of numbers that added up to $10$.

Definitely one of my better parenting days.

Assistant Professor of Computer Science at Hendrix College. Functional programmer, mathematician, teacher, pianist, follower of Jesus.
This entry was posted in teaching and tagged , , , , , . Bookmark the permalink.

### 66 Responses to Conversations with a six-year-old on functional programming

1. Ooh. I’m going to do this with my 6-year-old. Thanks for sharing!

2. blaisepascal2014 says:

Your game reminds me of the game Zendo. Mathematically speaking, in Zendo one player chooses a non-trivial subset of a universal set, and the other players try to figure out the rule governing the subset by asking if specific elements are in the subset. The main difference to me seems to be that while Zendo essentially concerns itself with deducing functions of the type $a \to 2$, your variant concerns itself with functions of type $a \to b$.

How did you deal with incorrect or vague guesses? For instance, if your son said that $\lambdax.10 \lfloor x/10 \rfloor$ was “round to the nearest 10?” how would you deal with that? The Zendo method would be to ask questions to clarify the meaning, like “What is 15 rounded to the nearest 10? What is 18?” until it’s clear what the guessed rule is, then provide a counter-example “18 rounded to the nearest 10 is 20, but my function turns 18 into 10”.

• Brent says:

I didn’t know about Zendo, that looks like fun. Yes, I think it is similar.

For incorrect guesses I just say it is incorrect, and often encourage him to ask about some other particular inputs which will serve as counterexamples. We haven’t really had issues with vague guesses yet. But yes, I think asking more questions to clarify the meaning and providing counterexamples is a great way to deal with such situations. This is definitely related to the variant I mentioned, where the guesser has to prove they understand how the function works by correctly specifying the output for inputs chosen by the other.

3. Noe says:

Great Idea for a game! Thanks for sharing!
(BTW: You’re trending #1 on YC Hacker News 😊)
https://news.ycombinator.com/

• Brent says:

Thanks, glad you like it! And I was aware of HN… I don’t read it myself but I could see that my blog stats were blowing up (more visits in the past 36 hours than I usually get in a year!).

• JM Giralt says:

I shared this on hacker news. I have a kid and thought this was utterly adorable. its turned out to be the 33rd most popular post of all time. http://www.hntoplinks.com/all/2, Great Post!

4. Glass says:

“usually because his idea of what the function does changes over time, which of course he, in all sincerity, denies”

He’s clearly going to be a project manager.

• kpodenphant says:

Excellent – Grinning from ear to ear :)

• Brent says:

Haha! =)

5. Gareth says:

If he’s engaged by this kind of thing, I can highly recommend Zoombinis, an educational game I loved as a child and recently got a remake.

It’s a game that does a really great job of teaching quite advanced logic puzzles while being charming and engaging. I can still play and enjoy it today as an adult (in fact, my mother still plays it from time to time after playing with us as children!). It introduces the concepts with easier versions and ramps up as you succeed. At the beginning you can just work from instinct, but as you go on you have to start constructing a mental model and adapting it as the puzzles get harder.

The remake is available for Win/Mac and Android/IOS and it’s cheap. https://external-wiki.terc.edu/display/ZOOM/The+Game

• Gareth says:

Just to give a bit more detail, you have a collection of Zoombinis, each of which has different traits (hair, eyes, noses, feet). The puzzles tend to be functions over these traits.

E.g: In the second level there are four caves (2×2 grid), and you must send Zoombinis to the right caves. So it might be say, red noses go left, others right, and glasses go top, others bottom.

In the “Fleens” level later in the game, you come across a group of creatures with different traits. Each Zoombini maps to a Fleen, and the aim is to work out the mapping function to select some Zoombinis matching target Fleens. To begin with it’s just red noses become purple noses, say, but as the difficulty ramps up you might need to understand that noses map to feet and hair is random.

This is all done in a simple way that’s really fun. without actually explaining anything like I’ve done here, it’s all just taught by letting the player experiment and deduce by generating hypothesis, testing them and rejecting or accepting them. Portal did a similar thing much later, but other than that it stands out amongst educational games for being both a ton of fun and truly teaching you something.

• Brent says:

This sounds great, thanks for the link! I had not heard of Zoombinis before.

Several years ago I heard Matthias Felleisen say something about a strong correlation between kids that understand functions at a young age and skill in mathematics later in high school and college. I started experimenting with my kids doing things similar to your game. Typically I’d draw a picture of some crazy contraption with an input and an output and then let them suggest different inputs to see what I’d say the output would be. They would then try to figure out what the machine did.

I also found that having them discover (and write down) their own truth tables for simple logic circuits in Snap Circuits was a helpful way to get them to think through the idea of figuring out what something does by trying different inputs.

I hadn’t considered trying to see if I could explain higher-order functions until I read your post, but I’m going to have to try that now.

• Brent says:

Yes, I was also thinking about higher-order functions… I think it would work just fine to play the function machine game with higher-order functions (“OK, you tell me a function machine from numbers to numbers, and I’ll tell you what number comes out…”), but it is definitely one or two notches up the ladder of abstraction and I don’t know if my son is quite there yet. If you try I’d love to hear about it!

7. Carl Hjerpe says:

Next up: The object inheritance game!

• Brent says:

What? How do you play that?

8. As sure as the sun will shine tomorrow, parents will completely fabricate intelligent conversations with their children

• Brent says:

While this may be true, I assure you I did not make up this conversation, though obviously I have no way to convince you of that.

9. Lucas Borges says:

Excellent game idea! I might play it with my friends

10. pokerface says:

I think your son would become a great famous person in the field, one who pushes forward the boundaries of knowledge. Mark my words!

(Whatever catches the interest of a very young kid tends to influence them for the rest of their lives).

• Brent says:

Perhaps! I don’t really care if he becomes famous; I will be happy as long as he always keeps a love for learning!

11. mikesub says:

That reminds me of https://en.wikipedia.org/wiki/Eleusis_(card_game)#Eleusis_Express which is somewhat the same, even with switching side to test hypothesis

• Brent says:

This is great, another similar game I hadn’t heard of! Thanks for the link.

12. coyoteclaws says:

This also reminds a lot of Elusis, a card game where one player makes a rule (and is “God”) and the other players must guess it (and are called “scientists”). It’s played with several decks of ordinary playing cards, and the rules must be based on those. If a player thinks they know the rule can become “prophet”, and must then judge the other players’ tries. https://en.wikipedia.org/wiki/Eleusis_(card_game)

• Brent says:

This is great, another similar game I hadn’t heard of! Thanks for the link.

13. Saarvi says:

My 8 years old is new to computers but is not so good in Maths, but I have to try this as I really like this way to make kids understand. Thanks!!

• Brent says:

You’re welcome! Have fun!

14. Josh Jennings says:

Awesome post! I have a 6 year old too and love teaching her concepts and maths. I’ll definitely use this.

• Brent says:

Cool, have fun!

15. Very inspiring! Thanks for sharing. That will be a good game to play on road trips.

• Brent says:

You’re welcome! I think you’re right that it is a great road trip game.

16. Somnia Lunatica says:

duuude – that is what great parenting is all about! wd!

17. Ewa says:

Wow, teachers could learn from you so much! Great teaching approach :)

18. Daniel Fava says:

Awesome! :-) I really enjoyed reading this story. I have a 4 year old and I can’t wait :-)

• Brent says:

Glad you enjoyed it! Yeah, 4 is probably too young for this game, but there are other fun math games you can do at that age (e.g. playing with different numbers of dots arranged in shapes like squares, rectangles, triangles…)

19. richardberg says:

• Brent says:

Looks fun, thanks for the link!

20. Great post and it definitely sounds like he is enjoying the game. Can’t wait to play this with my nephew soon :)

• Brent says:

Thanks — have fun! =)

21. WoW This is so cool. Someone actually told me long back that he has taught Functional programming to kids with great success. Excellent Post made me think how i would have perceived a concept such as this for the first time, “A Function machine inside a function machine”. Quite a crazy thought for a 6 yr old.

22. Your son might like my Function Game website where you can play the game with the computer:

• Brent says:

Thanks for the link! This is great work. I think it is too hard for my son yet, but I will save the link for later.

• Thanks. And I forgot to say how I really enjoyed your article!

• Brent says:

23. Well done sir! I’ll definitely try this with my 9 and 6 year old. I also got a smirk when I read about the mystery of functions that don’t pay attention to their inputs… I was looking at the 16 binary functions that take two inputs and produce one output recently, and it was the first time I realized that only 10 of them pay attention to both inputs, 4 of them only pay attention to one input, and 2 of them don’t pay attention to either input.

• Brent says:

Hah, good point, you’re right!

24. Michael says:

This post is awesome. My 10-year-old son was learning python and I was helping him. I tried to explain what function is. I told him function is like a factory(physical) – you deliver raw material and something comes out. Afterwhile – he insisted that it is a machine. We refused to see it my way and at the end I won. The joke in my family is ‘what is function?’ It is factory – starts with ‘f’. Now when you said a machine takes another machine and produces yet another machine. I think my son is right – it is machine. :) But I won’t tell my son.

• Brent says:

Hah! I’m curious, what were you thinking of the difference between a machine and a factory? Why did you insist on “factory” and not “machine”?

25. Aja Holubova says:

Thanks for the inspiration, we tried it with our kids on a car journey and it was a lot of fun. They grasped the concept surprisingly quickly.

• Brent says:

Cool, I’m glad it worked well!

26. ron says:

Brent, i really like the idea. i have a 6y old daughter, and 4y old son. The oldest is enthousiastic on the game. So, i tried making a browser verison for it. Its simple, none-screen-responsive and looks like a christmastree, but it works: https://github.com/ronstarcool/machine
plz check is out to see if you like it. If you have node installed, its very easy to run.

• Brent says:

Very cool, thanks for sharing!

27. fajarlaksono says: