Why I recommend playing with Haskell

I primarily program in Scala. A few years ago, I got into Haskell programming when the director of engineering at the company that I worked for told me about it and let me set up a Haskell study group. It’s a language which you probably won’t use in real life unless you work for a company that can afford to play with languages like these. Haskell gave me the first taste of FP. My coding style entirely changed once I got into it and learned pure functional programming.

Your perspective on programming changes completely once you play with Haskell. Programming without state, effects, and other things, and with everything being lazy, requires a lot of relearning, but it is definitely worth it. Reasoning about your program’s correctness can be completely achieved using the type system. Everything is a function – takes something in, gives something out. Debugging becomes incredibly easy because you know what each function will do. It is easy to trace bugs. You will fall in love with recursion. Before I learned Haskell, I was always a bit afraid of writing recursive functions. Haskell forces you to learn to write recursive solutions.

Fibonacci’s sequence is one of the problems that made me fall in love with Haskell. Let us see why. Fibonacci of n is defined mathematically as – Fn = Fn-1 + Fn-2 where n > 1. Simple enough, yes? Now let’s write it in Haskell. The function will be defined as:

fibonacci :: Integer -> Integer

That is pretty simple, right? Fibonacci is a function that takes an integer and returns an integer. We know that fibonacci of 0 is 0 and fibonacci of 1 is 1, so continuing the implementation:

fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1

It’s as simple as that. You can either continue defining fibonacci of each number, which would be weird, or you can take the formula and write the next implementation.

fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)

That is it! You have a function that can find the nth Fibonacci number. I know it’s an easy example, but once you get a hang of the language, everything will start to look as simple as this. Probably not incredibly simple, but you get the idea.

I absolutely love functional programming. There are concepts in Haskell that are very complex, but a lot of it is pretty nice to learn. Trust me, the way you program will entirely change once you learn pure functional programming. I’m sure there are other functional languages that you can learn, but Haskell forces you to think in a pure functional way.

Let me know why you love Haskell if you have already played with it. Cheers! This is what I used when I started - http://www.learnyouahaskell.com/