Functional Programming is an old paradigm that’s gained popularity in recent years due to its code conciseness, modularity, and lack of side effects.
Lisp is considered the first functional programming language. For newer incarnations, we can try Common Lisp or Racket.
What makes functional programming so unique?
Pure functions → The same input always produces the same output, and there are no side effects (no printing into the screen, mutating your input or changing the filesystem).
Function are First Class citizens → Function are treated as data. They can be used as variables, as input for other functions or as return values. Functions are everywhere.
Shorter code → Programs written in functional programming languages tend to be shorter than its counterparts like object oriented or imperative programming.
No loops → There’s no while, for, do while or anything of the sort as recursion is all you need.
What do we need?
In this post, we’re going to explore some key fundamentals of functional programming using examples in two different functional languages and you’re invited to install them both, or just install what you want or need.
Haskell (1990 / Purely Functional) → If we’re on a Mac, the best way is to use homebrew and type on the terminal window:
$ brew install ghc cabal-install
Where ghc is the Glasgow Haskell Compiler and Cabal is Haskell’s package manager.
For other systems, you can refer to the download page.
Erlang (1986 / Concurrent and Functional) → Again, if on a Mac, type:
$ brew install erlang
Or you can refer to the download page.
In functional programming, once a variable is assigned it can never change. You can think of that as using constants all the time.
In Erlang variables begin with an Uppercase letter and once assigned they cannot be changed.
In Haskell variables don’t follow any structure. Create a file called variables.hs and type the following:
a = 1
a = 5
Then open a terminal window on the same folder as your file and run ghci, then :l variables to load your program.
In short terms, pattern matching allows us to define how the same function will react to different parameters. This means that calling the function myFunction with no parameters will be different from calling myFunction with two parameters and different from calling it with five parameters and even different when calling it with different parameter values.
Let’s create an Erlang example called greetings.erl:
greet(male, Name) ->
io:format("Good morning Mr. ~s~n", [Name]);
greet(female, Name) ->
io:format("Good morning Mrs. ~s~n", [Name]);
greet(_, Name) ->
io:format("Good morning ???. ~s~n", [Name]).
When we run it, we can see that we can call the same function but using different parameters and it will always run as expected.
Anonymous functions are functions that do not have a name. Hence, they cannot be called from anywhere in the program. They are used to define functions that are not meant to be called multiple times.
Let’s see an example calling a function to manipulate a list. We’re going to write a file called lambdas.hs:
func :: Integer -> Integer
func num = num * 2 + 10
show_nums :: [Integer] -> [Integer]
show_nums lst = map func lst
We need to call the Haskell compiler to read our file:
$ :l lambdas.hs
And then call it like this:
$ show_nums [1,2,3,4,5]
While this is simple, we don’t really need the func function, so let’s change the source code like this:
show_lambdas :: [Integer] -> [Integer]
show_lambdas lst = map (\xs -> xs * 2 + 10) lst
And it will yield the same exact result, but with half the code lines:
A closure is a function inside a function that gets access to its parent function variables. Sounds strange, huh? Let’s see an Erlang example:
We’re going to write this example on a file called closures.erl:
GreetPerson = fun() ->
io:format("Hello and welcome ~s.~n", [Name]),
fun() -> io:format("Goodbye ~s. Nice to meet you.~n", [Name]) end
Greet = GreetPerson(),
To run this we need to compile the file first by typing on the terminal:
$ erlc closures.erl
This is going to generate a closures.beam file, which is a file that will be interpreted by the Erlang VM.
Once it is compiled, we need to open the Erlang VM and run it.
This will run and execute our code:
Here we can see that by calling Greet = GreetPerson(), we’re assigning the function GreetPerson() to the variable Greet. When we call Greet(), we’re greeting the person, but we’re also running an anonymous function that will say goodbye to the user. Notice that we’re not calling this anonymous function directly.
List comprehensions are a way to build or modify lists, and it is worth mentioning that lists are the most used type in functional programming.
Using list comprehensions, we could rewrite the lambdas example. This time, let’s use Erlang and create a file called list_comprehensions.erl with the following code:
[X * 2 + 10 || X <- List].
When we run this, it will return the same result:
Recursion is letting a function call itself, and this is not a foreign topic on non-functional programming languages, but the way it’s implemented and used is for sure different.
To make more sense of this, let’s see how a factorial source will look like in Ruby:
if n == 0
n * fact(n-1)
We can write the same on Haskell, like this:
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
The code is shorter and simpler:
And that’s it for a small introduction to functional programming.
Like what you read but looking for some Nylas content, make sure to check our great selection of blog posts.