Kids Coding, Part 2
They grow up so fast. Continue to follow along with this developer as he teaches his kids coding. This time we're tackling functions and variables.
Join the DZone community and get the full member experience.Join For Free
I didn’t expect to be writing the second blog post only 12 hours after the first. But when the kids came downstairs this morning, they were unbelievably excited to do more coding. So here we are!
Eliezer and I discussed using more visual tooling (like Code World or Jupiter) for doing the learning, and I showed him what it looked like. It seems he’s got quite a bit of his father’s preferences, and wanted to stick to plain text for now. We’ll probably circle back to that kind of stuff after they get the basics. It will also give me a chance to get more comfortable with those offerings. (Thanks all for the recommendations on Reddit.)
One final note. I’m extremely happy that we went with Haskell after today’s lessons. Concepts like variable replacement which are natural in Haskell were great teaching tools. I obviously don’t have enough data to prove this yet, but I’m more strongly believing the long-held theory that Haskell is easier for brand new programmers than those familiar with imperative programming.
I moved us over to VSCode, with the only syntax highlighting set up. Previously, I’d used my emacs setup with intero, and the red squiggles let them know they’d messed up too early. I used the docked terminal at the bottom, taught them to save, and showed them to press “up enter” in the terminal to rerun
stack runghc foo.hs. Perhaps not the ideal setup, but definitely good enough.
- Started with
main = print "hello world"
- Explained that
mainis defined as another thing
- Expanded to
main = print hello hello = "hello world"
- They were not surprised at all that this just worked
hello = 5 + 6, both got confused about whether it would print
5 + 6, so I reminded them of strings
- How about
hello = five + six?
- Both agreed it wouldn’t work. I typed in
five = 5, and asked if it would work. They agree that it won’t, and I show them GHC’s error message.
- “Variable not in scope.” Computers talk funny; this just means “I don’t know what six is.” Instilled that they’ll eventually be able to understand those massive error messages, but not too worried about it yet.
- Exercise: five the program. Both of them got it no problem.
- I was about to demonstrate
five = 4, and then Gavriella figured it out for herself! They understand that, again, “computers are dumb.” Names are just there for our benefit, the computer doesn’t care.
- We do
five + six * seven(with appropriate variable definitions), they get that answer, and then
(five + six) * seven, and they get that too.
- Now I define
fivePlusSix = five + six, and change to
hello = (fivePlusSix) * seven(direct replacement). They’re fine with this. Yay, replacement!
- Point out that the parentheses are now unneeded and remove them. Again, no problem.
- Parentheses just tell us “do this first”, but aren't not needed for one thing.
- Type in
hello = 5 * 2, no problem.
- How about
hello = double 5? They figured out that it won’t work because, again, computers be dumb.
- How do we define
double? Eliezer guessed
* 2, which is really close, but we’re not ready for sections, and I want to teach standard function stuff first.
- Show them
double x = x * 2, that
xis a variable, and a function argument. They don’t know algebra yet, but got this fairly quickly.
hello = addFive (double 5)
- Exercise: fix the program!
- Eliezer did this one, defined
addFive, and he started spacing things like me without any prompting. Cool.
- Exercise: Gavriella, write
- Got it the first time, ran it, and the answer’s still 15. Why?
- We stepped through how the program is evaluated. “Let’s look at
main, oh, I got an action! I need to print
hello, what’s that? Oh, it’s
addFive .... That means I gotta figure out
double 5? Oh, it’s
10. So that’s
minusTwowas never (terminology break, I taught them that it wasn’t used).
- Exercise: make the answer 13. They’re struggling to figure out where to use it. They’re stumped, time to jump in.
- Stepped through all of the types involved. “double is a function that takes a number and gives you back a number” blah blah blah
minusTwois also a function. It needs one number. Let’s try something wrong:
minusTwo addFive (double 5). That’s wrong: it means “apply
minusTwoto two values” but it only needs one number.
- We need to turn
addFive (double 5)into one thing. Ask them what we can use. They played with idea of double quotes, and then they figured out the parens! Awesome!
- One final exercise: make the answer 16 with
- Eliezer did that typing, so I gave Gavriella one real final one: make the answer 6 with
minusTen. She did it.
- Realizing now: learning to type and navigate an editor is possibly harder for them than the coding itself.
Gavriella kept playing with the code (wow, she’s focused on this). She decided she wanted to do division. That was more complicated, but she persevered. I taught her about
div being a function that takes 2 arguments. She didn’t know anything about remainders, and was confused that
div 15 2 worked at all. I taught her about
divMod, and she did great.
Published at DZone with permission of Michael Snoyman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.