Why I start teaching Clojure with Imperative Programming
Summary: LispCast Introduction to Clojure starts with 30 minutes of imperative programming. We write programs for their effects, so imperative is a great place to start.
I write about functional programming. I talk about it. I like the mathematical parts of it. And I think you should learn it. So it surprises people when they watch LispCast Introduction to Clojure and they spend the first 30 minutes at least doing imperative programming. I mean really imperative. Pick up a cup, scoop flour, pour the flour into the bowl, put down the cup, pick up an egg, crack the egg, drop the egg into the bowl, mix the bowl, pour it into the pan, pick up the pan, put it in the oven, ..... ^1 All of those actiony steps. Where's the data transformation? The function composition? The higher-order functions?
Here's the short answer: when teaching, start with the motivation. Answer why? before you answer what?.
Why do we program? To make things happen. Everything useful to us happens outside of the computer's memory. That might mean displaying something on the screen, sending some network traffic, or moving a robotic arm. That's what we're programming for: the effects. Functional programming is just there to help us do that.
So in Intro to Clojure, you're trying to help X5 (a robot who dreams of being a baker) make a cake and some cookies. It's a concrete task you can work toward and understand intuitively. And, while you're working on that, you're learning so much! The syntax, the semantics, the REPL, and more.
Once you're comfortable with that then we get to the functional stuff, in video 2. And maybe you are a little tired from baking so many cakes, so you really want to know how you can build some abstractions to help you out. Through progressive refactorings and abstractions, we develop things into a more and more functional style. It culminates in the third video with a full-blown data-oriented DSL for writing recipes. How much can be done with data is one of the deep insights that Clojure can give you. And I surprised myself by being able to teach it, step-by-step, in less than 2 hours.
- You may have noticed the baking metaphor. More on that later!