Click here to read second article of these series Introduction to Haskell Ranges

Let’s Start

Functional programming is getting a lot of buzzes recently with the rise of libraries like ReactJS developers are actually getting to know how much using the functional programming approach and Immutable data structures in javascript can actually improve how we write code for the web.

That does not mean how we write javascript right now is not a good practice but using functional programming concepts can actually help us to reduce side effects, write more readable code and even improve the reusability of our code.

That is the reason I started learning Haskell and if you programmed before in an imperative language Haskell will open new ways of learning and solving problems too.

The inspiration

For the first time, I ever got introduced to the concept of functional programming was when I started reading Functional-Light-JS by @getify.

I will recommend you to follow the guide above if you really want to leverage the power of functional programming concepts in your Javascript code.

Let’s get started

Let’s start by installing Haskell and GHCI prompt to our system. So we can actually test out our code.

These instructions are for MacOS. You can find out installation instruction for your desired system here.

Run this command brew install haskell-stack

And then this stack setup

if you don’t get any error you are good to go type stack ghci to run the interactive prompt.

That’s it.

Some facts about Functional programming languages

Let’s talk a little about Functional programming languages.

As the name suggests functional programming is done by creating a lot of functions and then composing those functions to create meaningful applications.

Everything is immutable

In a purely functional language, a function cannot have side effects

A purely functional language cannot have side effects in their functions. That means for a given argument the functions should always return the same result. for e.g if a function takes an input 2 and then output 3 it always should output 3 whenever 2 is passed to the same function.

Eliminating side effects from our functions increase our ability to reason about our program easier. We can easily reason what a piece of code do as we know our function does not depend on anything outside our function except our arguments.

Side effects free functions let us write predictable functions. We know ahead of time what will be the output of our function given an argument. ReactJS component render is a perfect example of a pure function.

Another pretty important fact about purely functional programming languages is that everything is immutable. If a variable defined once you cannot change its value. If a is 2 it will remain 2.

That sounds limiting. Yes, it does. But believe me, once you will start learning functional programming everything will make much more sense.

Let’s jump straight away into Haskell

Start you Haskell prompt by running this command stack ghci

Write a function in Haskell

Let’s write a function right away. In your prompt type

sumNumber x y = x + y

And then in the next line

sumNumber 4 5

We just defined a function and executed it. The sumNumber function takes two arguments x and y and returns their sum.

In Haskell function call syntax is a little bit different then other languages. Firstly you write the function name, space and then the function arguments separated by spaces.

Function definition syntax is relatively simple. You start by writing the function name, space, arguments separated by spaces, equal to sign and then the function body.

Infix notation

A really cool feature provided by Haskell is using your prefix function as infix. So what is an infix function? basically, infix function is called by sandwiching the function between arguments. For e.g + is an infix function

4 + 5

It takes two arguments one on the left and other on the right side.

But in Haskell you can call any function as Infix as long as the function takes two arguments. For e.g

sumNumber x y = x + y

Our sumNumber functions take two arguments. So we can use this function as an infix function. You can call a prefix function with an infix notation by surrounding the function name with backtick.

4 `sumNumber` 5

The output will be 9

If … else is just another expression

Okay, this will sound weird at first but it will make sense afterward. If … else in Haskell is just another expression. For e.g 5 + 4 is an expression it evaluates to something. The same applies for if else statements in Haskell.

Let’s explain this.

First of all else is mandatory with if in Haskell. Take a look at this example.

let a = 5

and then type

(if a > 10 then a + 10 else a + 5) + 1

The first part of our statement (if a > 10 then a + 10 else a + 5) evaluates 10 and then we add 1 to it. The output of executing the above line will be 11

As you can see the if else statements in Haskell just returns a result like any other expression.

Haskell uses a very good type inference.

That means Haskell can recognise the type of any variable without the need of explicitly defining the type. For e.g if you do something like this a = 10 + 15 Haskell will understand that a is a number

That’s it for this part. This was just an introduction. Click here to read Introduction to Haskell Ranges

I am documenting everything I am learning about Haskell here on Learning-Haskell-Documented on Github. Feel free to follow that guide or contribute if you find some improvements.