Home » Teaching topics » Functional Programming » What is Functional Programming?

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8 other subscribers.

What is Functional Programming?

Lambda symbol
Appears everywhere Functional Programming is mentioned

If you didn’t know, Computer Science A-level specifications are changing from September 2015. Computing is filtering through the Key Stages all the way to Primary school. It follows that some topics traditionally taught at University level are going to need to be pushed down as well if we are going to stretch the pupils we teach.

One topic that teachers seem cautious of is Functional Programming, (though only for those teaching the AQA specification). It doesn’t appear in the AS specification so it’s likely that most schools will only teach this in Year 13. The details in the specification stretch over 4 pages which some find daunting. I personally prefer that exam boards take this approach rather than omit details that are useful for medium-term planning.

It is my intention to build up a teacher guide to FP by stepping through the elements of the specification with some practical examples. Hopefully this will give you an idea of how to teach it in school.

So what is FP?

Functional programs execute by evaluating expressions that are functions. A function is something that takes data that is input and produces an appropriate output. Consider the function f(x) = x² where x is an input. Clearly we can get different outputs by changing the value of x we input, for example: f(2) = 4, f(3) = 9 and so on. Importantly different inputs can produce the same outputs (eg f(-2) = 4 and f(2) = 4) but importantly an input will ALWAYS produce the same output without altering any other data.

To achieve this, data within Functional programs is immutable. If you want some results to be stored then you need create new data structures. This often seems like a pain to your regular imperative programmer, (you could just update the same variable that was used before,) but it ensures that functions always produce the same results no matter how many times they are run. Also, Functional programs need to be stateless, that is they don’t remember anything that has gone on before. With these two principles you can be sure that each function will run in isolation and produce the same result every time no matter when they run.

Why bother?

WhatsApp on iPhone
Who said FP wasn’t used much?

FP shouldn’t be considered as a single one size fits all programming technique. Other paradigms are more suited to different tasks but FP excels when dealing with a lot of number crunching and data analysis. Financial software, social media services such as Facebook, Twitter and WhatsApp and not to mention eCommerce sites like Amazon all benefit from using FP derivatives. All of these generate huge amounts of data, (some would say Big Data,) and Functional programs cope well when processing this into useful information.

Other programming paradigms can achieve the same results but the features of FP languages enable tasks to be run concurrently without having to worry about threads that finish early or data that changes half-way through execution. The highly modular nature of Functional programs means that processing can easily take place over as many processor cores as are available resulting in a much improved execution time.

What I’ll use

I plan to develop example programs for the AQA specification using Haskell. It’s a widely used and truly Functional programming language. I will consider the wording of the AQA specification and develop examples that could be used to teach this with an A-level class. Before moving on I suggest you install it on the platform of your choice so you can try out a few examples.

Leave a comment

Your email address will not be published. Required fields are marked *