Functional Programming in Haskell

Haskell, the purely functional programming. It truly changes the way you code.

Image for post
Image for post
Rethink the way you code

Background story

The first thing that I notice when I begin to take a course in Functional Programming in Universitas Indonesia is “Hmm, how it can be done just like that?”. Turns out it is the reason why it is called functional programming, to change the old paradigm of imperative programming.

Functional programming

So what is the definition of functional programming? Functional programming is a style of programming which models computations as the evaluation of expressions. (Haskell Wiki). “Evaluation of expression” means we tell the computer what do we want. Unlike in imperative programming, we tell the computer how we able to solve the problem. A sample problem that can be used to show on these two style programming is simple problems like finding the maximum value in a list.

Functional programming vs Imperative Programming

In imperative programming we do it by, set a variable to hold the maximum value, iterating each element of the list, and then change the maximum variable if we found an element which bigger than the value that stored on the maximum variable. Here is the code in Python.

def max(arr):
maxVar = arr[0]
for i in arr:
if i > maxVar: maxVar = i
return maxVar

But if in functional programming / declarative programming. We can solve this problem by telling the computer maximum element from the list is an element which has the biggest value among the others. Implementation in Haskell can be done like this

maximum (x:xs)
| maxVar > x = maxVar
| otherwise = x
where maxVar = maximum (xs)

Above Haskell code implements pattern guards that can be used to check the given condition based on the argument and constant definition on ‘where’. If we run maximum [1,4,5], the execution begins with

maximum [1,4,5] ->

maxVar = maximum [4,5] ->

maxVar = maximum [5] ->

otherwise = 5 ->

(pop from the stack for previous function calling) 5 > 4 = 5 ->

5 > 1 = 5

maximum [1,4,5] = 5

Finally, we get the maximum element from the list. If you look at those implementations of solving the same problem, we realize that imperative programming and functional programming has a different way of solving the problems. On the imperative programming, we get used to mutating a variable. But on the other hand, functional programming or declarative programming doesn’t mutate any variables, we tend to declare a constant which an immutable state (can’t be changed). The benefit of not mutating a variable is that functional programming does not have any side effect on the execution of its function (that’s why it can be called pure function). A Pure function is when you have a function which you give its input ‘a’ it always returns ‘b’ and not others. Unlike in imperative programming, we can mutate a global variable, so when the global variable affects the function, the function can’t be a pure a function because there might be a condition where we mutate the global variables and got a different result than it’s expected. Therefore, Haskell is a language which has no side effects. It’s easy to test our code as we always know what value it will be returned.

Conclusion

Actually, there are other features in Haskell like High Order Function (HOF), Lazy Evaluation, Purity, Recursion, and etc (maybe I’ll write about it later :’) ). But in conclusion, if we look at the above sample, we can see that Functional programming makes our code in a more structured format by more reusing a function, and fewer code statements. All of those benefits lead us to the implementation of clean code and a better understanding of our code.

References

http://learnyouahaskell.com

Written by

Studying CS at University of Indonesia | Front-End Developer at my own company

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store