Ask HN: How to explain functional programming to my 13-year old?
Thanks to terrific programs and online resources like Codecademy, Scratch, and Khan Academy, my 13-year old has started to learn about HTML and CSS, as well as how to code in languages as diverse as JavaScript and Python.
I don't remember how we got on the topic of functional programming, but he asked me to explain it. So... I started down the path of comparing functional programming with imperative programming and then quickly realized I didn't know enough about functional programming to explain it properly :-)
How do you explain functional programming to someone just learning how to program? Would you suggest that someone completely new to programming start with a functional programming language like Erlang, Haskell, or Scheme instead of traditional, more imperative languages like Java, Python, and C?
(
15 comments
| 4 points
| on HN
)
I strongly recommend asking on the Racket-users Google Group as they'll know more about educational use than I, but Racket is actively used to teach 13 year olds programming in maths at school, and the basic Racket education program doesn't even teach mutation until much later.
The Racket group is here: https://groups.google.com/forum/#!forum/racket-users
I also know there are some youth education specific resources here: http://www.bootstrapworld.org/ And here: http://www.wescheme.org/
I might also recommend the Little Schemer, which presents and teaches recursive functional programming as if it were a book of puzzles. Realm of Racket is also pretty cool, though it does assume at least some programming knowledge already (it was written for first-year CS students), and you could try the early exercises in How To Design Programs as well (though I find them a bit long-winded and it may bore a younger programmer).
Good luck! I started programming at around that age, and I wish I'd had resources like Racket. If I'd discovered FP and Lisp then, I might've stayed with it instead of taking a break for ten years.
Running an imperative program comprise of setting up some initial state of all the variables and executing the steps in it, after which the state of all variables contain the result.
Running a functional program comprise of taking the input value, and following through the definitions and the end, you will have the result.
The difference between the two is that with imperative programs, there is a dependency on the state at every step. This makes it harder to write complex imperative programs. Functional programs does not have any steps, and hence no 'states' and hence eliminates this overhead...
a) variables can't vary once set
b) you don't get to have for loops (because variables can't vary); use recursion instead
c) function pointers are not frowned upon
d) there are no objects, which is nice in my opinion because OO is usually awful (exceptions being socket wrappers; it would be great to be able to handle a unix socket, a tcp socket and a TLS socket the same once they're fully connected)
Honestly, I wouldn't necessarily bring up functional programming, unless you're working on something where a functional language excels. I didn't know anything about functional programming (other than I had heard it was weird) until I started a job where we use Erlang, and it wasn't that hard to pick it up.
If you want to write a chat server or something, sure do it in Erlang, and learn about functional programming at that point.
As for a first language, my opinion is Racket unequivocally. The Beginning Student Language is entirely functional and the ecosystem offers a gradient from there to just about any point on the spectrum...from Typed Racket, Datalog, Lazy Racket and Algol 60...all within the same package, DrRacket.
Racket is also designed as a teaching language and extends that part of Scheme's mission. It has decades of educational research in its history.
Good luck.
Two resources I can recommend are:
- http://reactivex.io/learnrx/
- https://github.com/MostlyAdequate/mostly-adequate-guide
The second link in particular is quite an in-depth look at the more theoretical side of functional programming but in the first part it deals with fundamentals, and is not super complex to follow.
I say this as someone who can grasp the basics like functional composition, currying, partial application etc. but not stuff like monads, functors, etc. so take my advice for what it's worth :)
Good luck!
[0] - http://www.amazon.com/Introduction-Functional-Programming-Ca...
If your child already understands some OOP concepts like objects, classes, you could approach it from that perspective, that is, functions are special case of classes (and then explain why we want this). The whole Clojure language on top of JVM is built on this idea, so why not.
The most important take away for the kid is that these are programming paradigms, i.e. different approach used to solve the same programming problem.
(Also, if you are in the U.S. then as much flack as Java gets as a language, it is the language used for the College Board AP exam for Computer Science (https://apstudent.collegeboard.org/apcourse/ap-computer-scie...)
(OPs last paragraph)
I should of also added that Java is a good language to start out with due to schools teaching with that a lot, or the large portion of workforce that uses Java. So teaching functional programming (albeit Java is not strictly that) is doable using Java. But also has benefits of the AP test if OP and their kiddo are thinking college.
You can sugar-coat the story accordingly. It doesn't explain the entire concept, but gives a quick glance at the difference. I am not sure it's going to work or if it's exactly what you need. If you try though, I'd like you to share the results with the rest of us :-)
[1] https://gist.github.com/atmosx/85f5aced1f0f5859130a