Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How to explain functional programming to my 13-year old?
4 points by jamessun on Dec 10, 2015 | hide | past | favorite | 15 comments
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?




This is what Racket is made for (among other things of course).

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.


Imperative programs work in steps. It will be like, "Do this, then do this, then do this" etc, etc. Functional programming does not have 'steps'. A functional program is a series of definitions like, A IS B, B IS C and C IS D.

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


As someone who has been knee deep in Erlang for four years, functional programming is just like imperative programming except:

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.


The best explanation I've heard is Rich Hickey's in the Value of Values...i.e., what we usually want when we do something is the next value and changing something and then reading it is more complex than just getting it. I'm not necessarily saying sit your child down with the video, but watching parts of it with your child might be worth it...or watching it and explaining it.

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.


If he's already learning JS, why not stick with functional concepts there, in a language he is familiar with?

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!


I would recommend [0] An Introduction to Functional Programming Through Lambda Calculus. This does not require any mathematical background, starting from simple substitution through list processing. This is a good opportunity to introduce lambda calculus since the child is interested. I mean it, lambda calculus would be a good start.

[0] - http://www.amazon.com/Introduction-Functional-Programming-Ca...


I think that maybe you should start from some fundamental questions like why one should at all want to write in a functional programming language (immutable data, pure functions etc, and perhaps you have to learn it first by yourself).

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.


Go to project Euler. Do problems 1 to 5 in Java or Python and then do them in Scheme.

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.


I guess that he should be introduced to mathematical function rather soon in his curriculum. Maybe you should start from there. Function Composition is a mathematical concept as well.


My mother introduced me to Java when I was younger due to me making programs on my calculator to do my math homework for me. (Quadratic Formula: Input A, B, and C and outputs the answers, Infinite many solutions, or No solution). I continued to keep doing this with other mindless equations in Algebra, Geometry, Alg 2, and into Calculus.

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


The OP is looking for functional programming advice, so I don't see how Java is relevant.


"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?"

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


I didn't want post a huge comment so I made a gist[1]. This is an excerpt from Peter Cooper's book "Ruby: From novice to professional" which is the best ruby book for non-programmers IMHO.

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


wouldn't you try to teach him how to work in an functional language? He didn't "learn object oriented" programming, he just learned to code in languages built around it. i think understanding the difference between OO and functional comes after working with both for a while.


The Little Schemer ?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: