From 18bd9285ed215cf3a702bf75f8bb442ac16efffc Mon Sep 17 00:00:00 2001
From: Leon Byford
Date: Tue, 15 May 2018 18:48:33 +0100
Subject: [PATCH] functional programming post
---
....md => 2018-05-18-constants-can-change.md} | 0
...8-05-18-snoozy.md => 2018-05-25-snoozy.md} | 0
_posts/2018-06-25-functional-programming.md | 111 ++++++++++++++++++
3 files changed, 111 insertions(+)
rename _posts/{2018-06-18-constants-can-change.md => 2018-05-18-constants-can-change.md} (100%)
rename _posts/{2018-05-18-snoozy.md => 2018-05-25-snoozy.md} (100%)
create mode 100644 _posts/2018-06-25-functional-programming.md
diff --git a/_posts/2018-06-18-constants-can-change.md b/_posts/2018-05-18-constants-can-change.md
similarity index 100%
rename from _posts/2018-06-18-constants-can-change.md
rename to _posts/2018-05-18-constants-can-change.md
diff --git a/_posts/2018-05-18-snoozy.md b/_posts/2018-05-25-snoozy.md
similarity index 100%
rename from _posts/2018-05-18-snoozy.md
rename to _posts/2018-05-25-snoozy.md
diff --git a/_posts/2018-06-25-functional-programming.md b/_posts/2018-06-25-functional-programming.md
new file mode 100644
index 0000000..8defd86
--- /dev/null
+++ b/_posts/2018-06-25-functional-programming.md
@@ -0,0 +1,111 @@
+---
+layout: post
+title: If you know GCSE Maths, you know functional programming!
+tags: [programming]
+excerpt_separator:
+---
+
+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!
+
+
+
+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
+```
+
+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
+```
+
+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^{2} - 1
+Find f(-2) and g(3)
+
+```racket
+(define (f x)
+ (+ (* 3 x) 2))
+
+(define (g x)
+ (- (expt x 2) 1))
+
+> (f -2)
+-4
+
+> (g 3)
+8
+```
+
+> f(x) = 2x + 3 and g(x) = x^{2}
+Find fg(4), gf(4) and ff(4)
+
+```racket
+(define (f x)
+ (+ (* 2 x) 3))
+
+(define (g x)
+ (expt x 2))
+
+> (f (g 4))
+35
+
+> (g (f 4))
+121
+
+> (f (f 4))
+25
+```
+
+Piece of cake. :-)
--
2.22.0