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 Voigtlä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.

About Brent

Associate 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.

83 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/

  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.

  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.

  6. Mark Shead says:

    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.

  7. Carl Hjerpe says:

    Next up: The object inheritance game!

  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. Pingback: Link of the Day: May 7 2018 – skef.blog

  10. Lucas Borges says:

    Excellent game idea! I might play it with my friends

  11. 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).

  12. 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

  13. 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)

  14. 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!!

  15. Pingback: Interesting Links for 08-05-2018 | Made from Truth and Lies

  16. Josh Jennings says:

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

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

  18. Somnia Lunatica says:

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

  19. Ewa says:

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

  20. 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…)

  21. richardberg says:

    Sounds like your kid would enjoy Funville Adventures! https://www.amazon.com/Funville-Adventures-Fradkin/dp/1945899026/

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

  23. 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.

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

    http://www.functiongame.com/

  25. 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.

  26. 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”?

  27. 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.

  28. 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.

  29. Pingback: Newsy programistyczne 2018-05-13 – DevNation

  30. fajarlaksono says:

    This is nice, i like this article and your idea.

  31. Gabriel says:

    Truly wonderful the mind of a child is. – Yoda

  32. Michael says:

    This is so great.

  33. Pingback: Carnival of Mathematics 158 | The Aperiodical

  34. Andrea Garbagnoli says:

    What about a logic programming language for children?
    https://github.com/ToonTalk/ToonTalk
    http://www.toontalk.com/English/papers.htm

  35. Pingback: Resumen de lecturas compartidas durante mayo de 2018 | Vestigium

  36. MysteriesOfLife says:

    This is so adorable! I love the sound of teaching little kids concepts of programming like this. I teach after school programs that use a website called Scratch but I think making games like this would introduce the younger generation to the coding world a lot easier 😮

  37. Dave says:

    This is so nice :) I’m looking forward to trying it out on my own son when he gets a bit older!

  38. Pingback: 2018 Year in Review: Top Stories in Tech – Vitor Freitas

  39. Anonymous says:

    Sounds like you’ll soon be playing with alligators and eggs:
    http://worrydream.com/AlligatorEggs/

  40. Anonymous says:

    Such a beautiful story about love and passion. Thank you for sharing this inspiring annecdote !

  41. Satvik says:

    Have you heard or watch a TV Series called “Person of Interest”? Forget about the spying part, but, the “Machine” in the TV show is something like this. It takes in all input and gives an output :)
    I am a Math fanatic, I love what you are doing with you kid. :)

  42. Just came across this and with the current environment we are in – this is just truly wonderful. I can only imagine how far your son has come since. Thanks for sharing.

  43. Conor Neill says:

    Brilliant! (On day 40 of quarantine… running low on my own pedagogical ideas!)

  44. Dexter says:

    I just wanted to let you know that as a tech lead I often had trouble explaining junior programmers how functions work. Your post has helped me immensely. Sometimes knowing something intuitively isn’t enough to teach it to others. Thanks!

  45. Pingback: Newsletter #2 05/2020 – O. Valadares' Blog

  46. Anonymous says:

    Love this story!

  47. Hooman says:

    I need one of these fathers xD

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.