Can you teach a school class functional programming?

Madeleine Malmsten
4 min readNov 7, 2018

--

Earlier this year, my colleague Tom Dahlen and I got the idea of trying to teach a school class functional programming. We’ve both been programming since we were kids, and we wanted to give others the possibilities that we got from finding this weird and geeky hobby at a young age.

Therefore, I contacted my math teacher from junior high school to discuss the possibility of borrowing a class for a day and teach them how to code. He thought the idea was excellent, and we set a date and a plan for how to proceed.

“three persons in front of table” by rawpixel on Unsplash

What makes programming interesting?

If you have ever tried to understand Monads, then you know the feeling of seeing them everywhere! We wanted the students to get the same feeling, to start finding that programming is the backbone of their mobile phones, computer games, and social media accounts.

We also wanted them to be able to see programming as a tool to build things and to understand the benefits of knowing how to code.

What we did

No, we did not teach them monads, but we wanted to show them something that would be considered a few levels more advanced than the “drag-and-drop-learn-how-to-code-apps”. So we taught them how to communicate with each other via Erlang nodes.

What is a variable?

Firstly, they had to learn the basic terms; such as types, statements, variables, etcetera. The students followed the instructions and tried for themselves in the Erlang console.

X = 1
Y = 2
X + Y = 3
Mat = “Bigmac”.
Bigmac = “good”.
Mat == Bigmac => false
Mat == “Bigmac” => true
Lista1 = [1, 2, 3].
Lista2 = [“jag”, ”heter”, ”Tom”].
Lista3 = [“en blandad lista”, 23, true]
lists:sum(Lista1) => 6
1 == 1 => true
1 == 2 => false
2 > 1 => true

When the first error messages popped up the chaos was real, but after a few error messages the students started to understand that it wasn’t that dangerous to make an error — and the noise level went down a notch.

“photo of gray mountain” by Gary Saldana on Unsplash

We finished the first class with presenting functions, using the linear equation as a reference.

f(x) = kx + mk = 10
m = 2
f(2) = 22
linear_equation(X) ->
K = 10,
M = 2,
K * X + M.
Y2 = linear_equation(2),
Y2 = 22.

Distributed systems

After a quick lunch break, we started coding for real.

One of the best features of Erlang is the easily done communication between nodes, so our second task was to set up a small distributed system together, enabling the possibility to send chat messages to each other. The students were surprisingly patient and instead of making frustrated or terrified sounds like earlier, we heard some cheering here and there when they managed to connect to each others nodes.

The script was very simple, but included quite a bit — like a mail box, a recursive function, and a new process.

-module(skola).
-export([start/0, message/0, send/2]).
start() ->
register(message, spawn(skola, message, [])).
message() ->
receive
{Pid, Message} ->
io:format("~p says: ~p~n", [Pid, Message]),
message();
_ -> message()
end.
send(Message, Pong_Node) ->
{message, Pong_Node} ! {self(), Message}.

One student found the official Erlang documentation and figured out pretty fast that there was something called recursive functions, and managed to spam the chat with the current timestamp every second.

Conclusion

According to the evaluation form the students got to fill out after the class, they enjoyed the day. They especially liked writing messages to each other – everyone got to be involved, and they found it easier to keep up, even for those who did not clearly understand the programming principles.

Even thou not all of them will go for programming in high school, a few of them showed great interest in becoming programmers and wanted to get more info on how and where to start coding.

What amazed us the most was the sharpness of the students and how quickly they understood and accepted the core of the functional paradigm. We had tons of fun, and we would love to do it again!

--

--

Madeleine Malmsten

Developer / Staff Engineer with a soft spot for functional and logic programming.