2018-07-30-functional-programming.md 2.91 KB
Newer Older
Leon Byford's avatar
Leon Byford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
---
layout: post
title: If you know GCSE Maths, you know functional programming!
tags: [programming]
excerpt_separator: <!-- more -->
---

It took me an unbelievably long time to grasp functional programming. In the lectures I took, it was described in terms of lambda calculus with lots of weird mathematical symbols that didn't make sense to me.

However, little did I know that I had already learnt functional programming back in GCSE Maths, without even knowing it!

<!-- more -->

On the BBC Bitesize website, there is [information on using algebraic formulae with function notation](https://www.bbc.com/education/guides/z36vcj6/revision/6) [^0].

[^0]: The linked page is for the Edexcel exam board, but there are similar pages for [AQA](https://www.bbc.com/education/guides/zqpfcj6/revision/6) and [Eduqas](https://www.bbc.com/education/guides/z8cqpbk/revision/6).

As you can see, you can define a formula like:

> f(x) = x + 1

This lets you substitute x with any number. For example, x=3:

> f(3) = 3 + 1 = 4

Actually, this works exactly the same way when doing functional programming. For example, I can create a function in Python with the same functionality, which we invoke like so:

```py
> f(3)
4

> f(5)
6

> f(-2)
-1
```
Leon Byford's avatar
Leon Byford committed
38
{:.console}
Leon Byford's avatar
Leon Byford committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

Defining this function is different from how we do it in maths, but it's relatively intuitive:

```py
def f(x):
  return x + 1
```

Isn't this like just any other Python function? Why, yes it is. But the key thing is that the functions in functional programming have no side-effects. We take the parameters, transform them in some way, then return the result, without modifying any external variable. As such, whenever we call the function with the same arguments, we should expect to always receive the same result.

So, how do we do this in Lisp/Scheme/Racket? It's actually quite similar. But note that these languages use prefix notation -- not infix -- so there is a different syntax for calling functions.

Instead of `f(x)`, we write `(f x)`. Really, all we've done is brought the `f` inside the brackets, and added a space to separate it from the `x`. So, to define the same function as before:

```racket
(define (f x)
  (+ x 1))
```

You'll notice that `+` is also a function that accepts multiple arguments.

So now we can call the function like so:

```racket
> (f 3)
4

> (f 5)
6

> (f -2)
-1
```
Leon Byford's avatar
Leon Byford committed
72
{:.console}
Leon Byford's avatar
Leon Byford committed
73 74 75 76 77 78 79 80 81 82 83 84

And that's functional programming in a nutshell! That wasn't so bad, was it? Now, let's solve some of the problems given on the BBC Bitesize page using Racket:

> f(x) = 3x + 2 and g(x) = x<sup>2</sup> - 1  
Find f(-2) and g(3)

```racket
(define (f x)
  (+ (* 3 x) 2))

(define (g x)
  (- (expt x 2) 1))
Leon Byford's avatar
Leon Byford committed
85
```
Leon Byford's avatar
Leon Byford committed
86

Leon Byford's avatar
Leon Byford committed
87
```racket
Leon Byford's avatar
Leon Byford committed
88 89 90 91 92 93
> (f -2)
-4

> (g 3)
8
```
Leon Byford's avatar
Leon Byford committed
94
{:.console}
Leon Byford's avatar
Leon Byford committed
95 96 97 98 99 100 101 102 103 104

> f(x) = 2x + 3 and g(x) = x<sup>2</sup>  
Find fg(4), gf(4) and ff(4)

```racket
(define (f x)
  (+ (* 2 x) 3))

(define (g x)
  (expt x 2))
Leon Byford's avatar
Leon Byford committed
105
```
Leon Byford's avatar
Leon Byford committed
106

Leon Byford's avatar
Leon Byford committed
107
```racket
Leon Byford's avatar
Leon Byford committed
108 109 110 111 112 113 114 115 116
> (f (g 4))
35

> (g (f 4))
121

> (f (f 4))
25
```
Leon Byford's avatar
Leon Byford committed
117
{:.console}
Leon Byford's avatar
Leon Byford committed
118 119

Piece of cake. :-)