The difference between imperative and functional languages- 3 mins
When we are moving from one paradigm to another it's difficult to learn how to start thinking from a different viewing point. Think about it: when you had your first classes at college about C and then in the next semester you needed to learn C++ or Java and its classes, hierarchy, composition, constructors and so on, it should have taken a little while from thinking in a procedural way to thinking code in a OO architecture. What about moving from OO designed architecture to functional programming? Let's see.
I'll describe in this post the most important breaking point in these early studies about functional programming using Haskell until now.
No more blablabla:
In imperative languages (you use at least one of them if you are here) - such as C/C++, Java, Golang - you describe HOW TO achieve your desired data from an input.
In the other hand, functional languages (like haskell, elm-lang, etc.) you describe WHAT IS your desired data for a certain input.
This can be a little bit abstract, let's see an example.
First of all I'll define my dictionary this way:
A list of pairs (pair: tuple containing two values) where the first one is a string for the person's name and second one is its age
So in C++ I would define a tuple like this
*Assuming we don't have all lovely functional tools from C++14 (lists, tuples, ...). I'm trying to describe an example for any imperative language and while some have functional tools (c++14, python), others don't (c, golang)
Then my dictionary instantiated on main would be:
Take a breath, we will continue after that. I know it's getting boring, but stay with me a little bit more.
At last, for getting a person's age from my dict structure I must search like this:
My outputs are then
Ok, this was a lot of work, you can check complete code at this gist
We defined our structure, and HOW TO operate on that.
Well, the fun starts now.
I'm assuming you're not familiar with Haskell syntax, so I'll be a little verbose.
Let's get back our dict definition
A list of pairs (pair: tuple containing two values) where the first one is a string for the person name and second one is its age
For haskell lists and tuples are concepts well defined, because, hm... it's a functional language.
Note: GHCi is a haskell interactive shell, something pretty much like python interpreter
So our dict would be simply:
A list of pairs!
Nice, we defined our structure. Despite we don't had to define the tuple type like in C++, things are going in the same way.
Step 1: Filter your list removing undesired elements
*I've saved my hs file as
Now we have a list of elements that interests us.
In C we defined that first element containing the key returns its value, let's define same behavior in haskell.
Step 2: Take only first element from a list. You just need to head list
Ok, now we got the first tuple in our dict with
Step 3: Returns age from pair (name, age)
Since we want just its age, then
filterByKey changes to getAge
Voilà, we've extracted the data we want from our structure saying WHAT our desired data is, not HOW TO obtain it.
First mistake that people like me who are starting a new paradigm make is trying to associate the known thinking process to a completely different thing that doesn't have anything to do with the old paradigm.
So, next time you're writing a functional program think: "What's my output and what functions must I apply to my input to produce the data I want?" instead of "What steps are necessary to achieve my goal?".