Functional Programming
I’ve been into Functional Programming since I started programming, and since this is a topic very close to my heart, I’d like to write a little bit about the Functional Programming Paradigm, what it’s all about, and the main differences compared to the Imperative Programming.
Paradigm?
A programming paradigm is classifying different programming languages based on the way the software is constructed.
There are a few different programming paradigms, and the most common one is Imperative Programming, a paradigm that changes a programs state using statements. What the program does is giving the computer a description of how to solve a problem using a sequence of steps. This way of coding is often done using Object Oriented Programming. Examples of imperative programming languages are Java and C#.
Functional programming is a declarative paradigm where instead of changing the bytes, bits, and strings in the memory, the program tells the computer more abstractly, what things are and what the problem is. Examples of functional programming languages are Erlang and Haskell.
Functional?
In order to get an understanding of Functional Programming, it’s good to start with the basic match functions. In mathematics, a function is an expression involving one or more variables. Basically, mapping from input to output, like this:
f(x) = 2xx = 1
f(x) = 2
And when the expression involves more variables, it could look like this:
g(x, y) = x + yx = 1, y = 2
g(x, y) = 1 + 2 = 3
And you can also combine different functions, as follows:
h(x, y) = f(x) + g(x, y)x = 1, y = 2
h(x, y) = f(x) + g(x, y) = 2 + 3 = 5
According to mathematics, functions can’t change their inputs. This means that the value of x will always be the value of x, no matter what the function looks like.
Benefits of Functional Programming
Immutability: Functional Programming is a paradigm deeply rooted in mathematics, with one fundamental principle; All computation is the execution of mathematical functions. And since mathematical functions can’t change their inputs, all variables have to be immutable (a variable can’t be modified after it’s created). This makes the code more predictable and concise compared to Imperative Programming.
Pure Function: Continuing on the immutability and mathematical functions track, the functions in Functional Programming are transparent to the user, and doesn’t change any data outside the specific function.
High scalability: Considering the two benefits above, implementing concurrent events is very easily done.
Lazy evaluation: The evaluation of an expression is delayed until its value is actually needed.
High readability: In Imperative Programming, the methods could sometimes be pretty long and nested, something that is not that common in Functional Programming.
Concrete differences in code structure
In Functional Programming if … else is often replaced with pattern matching. Say that we want to run function foo1() if x is true. Otherwise, we want to run foo2(). This is how it would look like in JavaScript:
function myFunction(x) {
if(x) {
return foo1();
} else {
return foo2();
}
}
Following is how it would be done in Erlang using pattern matching:
my_function(true) -> foo1();
my_function(_) -> foo2().
A for or while loop would require a mutable variable, and therefore, there are no for loops in Functional Programming - this is replaced with recursion. Let’s make another comparison. JavaScript:
function myFunction() {
for (var i = 1; i < 10; i++) {
doSomething();
}
}
Erlang:
my_function(10) -> ok;
my_function(X) ->
do_something(),
my_function(X + 1).
So, how do I get started?
There’s, first of all, a lot of guides and documentation to be found, and since the Functional Programming community is a very nice one, I’d recommend going to a meetup or two. There are a few conferences I’d recommend as well.
https://learnyousomeerlang.com/
http://erlang.org/doc/search/
https://www.meetup.com/
https://www.codesync.global/
Happy coding! :)