Function programming is based1; that is, based on principles that allow a systematic and repeatable process for creating software. In this post I'm going illustrate this process with an example of summing the elements of a list, inspired by this conversation. We'll mostly be looking at algebraic data types and structural recursion (which often uses pattern matching, but is not synonymous with it).
For me, a working functional programmer, this process is one of the main advantages of FP. It means I can spend my mental cycles on understanding the problem, knowing that once I have done so the implementation follows in a straightforward way. The inverse also holds: if someone uses these principles to write code I can easily work out what problem it solves.
In this article I'm going to go through the process of improving some code. I'm mentoring a new developer who is applying for their first job. They were asked to complete some tasks on Codility as the first step of the interview process. To get used to the platform they did the first example task, and I advised them on some changes. I'm writing up here the progression from their code to (what I think is) better code. (Since this is the example task, not a task used to assess applicants, I think this is ok to publically post.)