Basics of functional programming
For a more in depth overview of Functional Programming at A-level (and beyond) please take a look at Richard Pawson's Computer Science from the Metal Up |
The material for this page was originally written for isaac computer science |
Functional programming is a declarative programming paradigm, where programs are written as mathematical functions whose order of execution is not solely defined by the programmer. These mathematical functions produce consistent outputs solely dependent on the inputs. Functions can be constructed out of other functions, or call themselves recursively. Functional programming is different from the imperative programming paradigm in several ways:
The outputs of functions only depend on the inputs, and not the changing state of a program, functional programming is stateless. For example, the python program below calls the same function addNumbers
twice with the same parameters 2,2
, but different values are returned because the state of the program (here simulated by the variable c
) changes. This wouldn't happen in a functional programming language, as whenever you call a function with the same arguments the same output would be produced.
Python example | Haskell example |
---|---|
def addNumbers (a, b):
return a + b + c
...
print(addNumbers(2,2)) # prints 8
...
print(addNumbers(2,2)) # prints 6
|
addNumbers a b c = a + b + c
...
addNumbers 2 2 2 --returns 6
...
addNumbers 2 2 2 --returns 6
|
not functional programming as addNumbers usesthe global variable c |
functional programming, the value returned byaddNumbers is only dependent on the inputs
|
Functional programming languages avoid side effects. A side effect is where a function modifies something outside its local environment. In the example below the function fudgeNumbers
changes a global variable g
when it is run.
Python example | Haskell example |
---|---|
g = 0
def fudgeNumbers (a, b):
global g
g = a + (2 * b)
return a * b
print(g) # prints 0
print(fudgeNumbers(2,2)) # prints 4
print(g) # prints 6
|
g = 0
fudgeNumbers a b = a + b
g --returns 0
fudgeNumbers 2 2 --returns 4
g --returns 0
|
not functional programming as fudgeNumbers changes the value of a global variable g when run |
functional programming, |
Functional programming also avoids using variables that change at runtime, data structures are considered to be immutable.
Python example | Haskell example |
---|---|
a = 2
b = 5
c = a + b
a = 6
|
a = 2
b = 5
c = a + b
|
not functional programming as it changes the value of a at runtime |
functional programming, variables are only declared once |
Benefits of the functional programming paradigm include:
- supporting the decomposition and abstraction of computing problems into functions that are made up of other functions which are made of other functions and so on.
- easier to read and understand code.
- easier to debug code as each function can be tested separately and does not rely on the state of the machine.
- lending themselves to running parallel and concurrent progam execution, allowing functional programs to run on multi-processor systems.
Lisp was one of the first functional programming languages, being created in 1958. Several dialects of Lisp are still in common usage including Common Lisp and Scheme. Other functional programming languages include Erlang and Haskell. Whilst imperative programming languages are more commonly used than pure functional languages, it is possible to do functional programming with many modern languages. Other languages that support functional programming include Python, C#, F#, Java, Visual Basic .NET and JavaScript.
Scheme example code |
---|
(defun factorial (n)
(if (= n 0) 1
(* n (factorial (- n 1)))))
|
Using Haskell for the first time
[edit | edit source]The majority of the code examples in this chapter will be in Haskell. You can learn more about Haskell over at Learn You a Haskell for Great Good!. To execute the examples shown in this chapter you can download the open source Glasgow Haskell Compiler or GHC. Most examples can be written directly into the command line interface that the GHC gives you, below this is shown with the .>
symbol. For example:
.> putStrLn "Hello World!"
Hello World!
.> 4 ^ 7
16328
.> [1..8]
[1,2,3,4,5,6,7,8]
.> round 7.8
8
Later examples might require you to write multiline programs, especially when you need to declare your own functions, to do this you need to open a text editor and save your Haskell file with the .hs
extension. For example:
myFirstHaskell.hs |
---|
myFactorial :: (Integral a) => a -> a
myFactorial 0 = 1
myFactorial n = n * factorial (n - 1)
|
We can then load this into the GHC by using the :l
(load) command, followed by the file location and name.
.> :l myFirstHaskell.hs
[1 of 1] Compiling Main ( myFirstHaskell.hs, interpreted )
Ok, modules loaded: Main.
Now we can use the loaded function, myFactorial
, from the command line:
.> myFactorial 4
24
Other Haskell development environments are available including repl.it |
Exercises: Funcational programming fundamentals What is a functional programming language? Answer: A functional programming language is one where code has no side effects, the program execution is stateless and the outputs of functions only depend on the inputs. Describe two of the benefits of using a functional programming language Answer:
Explain why this wouldn't be considered an example of a piece of functional programming? ...
def multiply (a):
return a * b
...
Answer: We don't know what value
|