Let’s start to use functional programming! — Part 1
In this simple blog post — the first of a three-part series — we’re going to introduce functional programming.
I am using Bing Chat to boost the time and test its help in content writing.
(Also the image is created using Image Creator powered by DALL·E)
Functional programming is a programming paradigm that focuses on binding everything in pure mathematical functions style. It is a declarative type of programming style that focuses on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.
In Functional Programming, a program consists of the definition of a set of functions, which can call each other. Running a functional program consists of calculating the value of a given expression, simplifying it as much as possible.
Functional Programming is based on the reduction computation paradigm, according to which one calculates by reducing an expression to another that is simpler or closer to a value, i.e. to an expression that cannot be further simplified.
Functional languages refer to the computational model of λ-calculus and the concepts present in imperative languages, so natural for many of us, are completely absent (or take on a different meaning). Instead we find notions such as:
- expression
- variable — to be understood in the mathematical sense: — unknown entity — abstraction of a concrete value
- validation different from the notion of execution: when evaluating a mathematical expression we do not change anything
- recursion
Let’s see some examples
(we will use OCaml as programming language using TryOcaml)
Sum Two Numbers
let sum a b = a + b
TryOcaml’s interactive prompt gives us the following evaluation
What is the meaning of sum: int -> int -> int = <fun> ?
We have declared a function (<fun>) that receives two integers as input (int -> int) and produces an integer (->int) as output.
The expression int -> int -> int = <fun> is called type schema.
If we evaluate the following expression
sum 5 3
The result is as follows
What if we evaluate this one ?
sum 5
Will we have a mistake?
Surprise!
sum 3 is a function! (sum 3) is the function that if we give an integer value a as input, returns 3+a
Max value betweem a and b
let max_value a b =
if a>b
then a
else b
TryOcaml’s interactive prompt gives us the following evaluation
This return is very similar to the previous one.
What is ‘a ? We are considering polymorphism!
max_value is a function that takes two inputs of the same type and outputs a value — also of that type.
In general, the sum function should have had this type schema as well but the ‘+’ operator forced the type of the inputs to be integer.
Pairs
OCaml has the tuple type.
Let’s see how to define a tuple of two elements.
Note the * symbol.
Let’s see how to declare a function that adds two elements of a tuple.
let sum (a,b) = a + b
Again, note the type schema int * int -> int.
Sum is a function that receives as input a pair and not two values. Indeed...
For now that’s all.
In the next blog post we will delve into recursion and pattern matching, working with lists and discover the true potential of Functional Programming.