DEV Community

Cover image for 5 (practical) reasons why your next programming language to learn should be Haskell
mpodlasin
mpodlasin

Posted on • Updated on

5 (practical) reasons why your next programming language to learn should be Haskell

I have read once that a "real" programmer should learn at least one new language every year. I don't necessarily agree with that (after all, there are more important things to do in life than just code all the time). But I do agree that learning a new language can bring a tremendous benefits to your programming knowledge and style.

This effect however diminishes if you learn only programming languages that closely resemble the language you already know well. The thing is, most of the popular languages nowadays really differ only in details, like some syntax or special features, but not in the general approach.

That's why I believe learning Haskell should be a top priority for a programmer who wants to push himself and gain new knowledge and expertise. Especially if he/she wants to seriously get into functional programming (but as we will see, object-oriented programmers will also gain a lot from learning that language!).

I don't believe that after learning Haskell you will want to use it in your daily work. For practical purposes I believe it is still a bit to alien and restrictive. But I do believe that there are actual, very practical benefits of learning that language. In this article I will try to describe those benefits, so that you can be sure that time invested in learning Haskell will be well worth it.

So let's get started!

What is Haskell?

Now, before we really begin, I should probably explain at least in few sentences what Haskell is and what makes it so different.

It's actually kind of a difficult thing to do, because Haskell has multitude of characteristics that make it different from your typical programming language.

Haskell is:

  • Functional and immutable - this is probably the least alien thing for a modern developer. If you like functional programming, you will feel at home while learning Haskell. All it's data structures are immutable by default and it supports many functional programming patterns out of the box, most notably currying.

  • Lazily evaluated - In Haskell, defining a value doesn't actually mean it will be created in memory. Haskell is "lazy", which means that it will compute it's values only when absolutely necessary. This results in some mind-bending properties, like, for example, creating lists of... infinite lengths!

  • Strictly typed - There is actually a lot of functional programming languages out there, but Haskell differs from them with its incredible type system. After learning Haskell, every other type system, like the ones in Java or TypeScript, will seem ugly and clunky to you!

  • Side-effects free - Now this is a bit of a lie but, at least conceptually, Haskell is side-effects free. This means that in Haskell you can't just print a thing to a console or make a HTTP request. Hell, you can't even change a content of a variable! All actions like these - called side effects - are carefully encapsulated in Haskell. This results in some interesting properties - for example simply by looking on a type signature of a function you can tell if this function performs some side effects!

If you have never programmed in a language like Haskell, many of those adjectives might still sound very alien to you. And that's okay! Part of the fun of Haskell is learning the concepts that I have listed. In fact, it's precisely those concepts that will make you a better programmer even when you use a more traditional programming language!

So with the introduction out of the way, let's finally find out why would it be worth learning this bizarre language:

1. You will get a better handle on programming with types

As I mentioned, Haskell's type system is simply incredible.

On one hand it's extremely strict. It's very hard to cheat it. For a beginners this is often the thing that scares them away from Haskell. At the beginning you will find yourself spending more time writing type definitions than the code itself. It will seem slow and frustrating.

But this effort will quickly pay off, because the type system is so powerful, that if your code actually compiles, it means that it probably works. Within a reason of course, but one of the pleasures of Haskell is how much trust you begin to put in the compiler at some point. This differs from regular languages, where - even after compilation ends without errors - you still can never be sure if your code won't crash.

And although the type system of Haskell is strict, it's also extremely expressive and has very lightweight, elegant syntax. Reading the types of functions is actually a pleasure.

On top of that, the compiler is so smart, that very often you can omit type definitions altogether. You can basically write a code that looks almost Python-esque!

But I wouldn't recommend writing Haskell in this way. After all, we are here to learn something. The thing is, Haskell's heavy focus on types will very quickly affect how you approach writing code in general. Very often, when you find yourself lost in some problem, you will begin by writing type definitions for your not-yet-existing code.

It makes it possible to sketch your application before actually coding it. And the best thing is that this sketch can later actually be compiled, so that you can check if your ideas even make sense.

This way of types-first thinking is something very powerful. After writing in Haskell long enough, you will soon find yourself programming this way in other languages as well.

And funnily enough, this is not something you have to consciously think about when coding in Haskell. It just happens by itself, because it's simply easier (and more fun!) to write the code this way.

2. You will learn what reusability should really look like

The expressiveness of Haskell's type system makes it also very easy to write code that is extremely general. This, plus a massive standard library with basically all the utilities you might ever need, makes resuability in Haskell a fact, not a wishful thinking.

After getting proficient enough and learning some (seemingly abstract at the beginning) concepts like functors or monads, you will discover that writing even very complex code can often be delegated to composing it from a 3 or 4 functions from the standard library. As you progress in learning Haskell, you will find out that your code becomes progressively shorter, simpler and it involves less and less own, custom code.

"Abstractness" and generality of Haskell is another thing that makes people initially apprehensive when learning that language. But it will be a lesson for you how to write code that is reusable on a level that you have never seen before.

3. You will learn programming with interfaces

This is basically the same point as before, but with more emphasis towards people who would want to learn how to code in object oriented style.

Funnily enough, in my opinion, one of the best ways to learn how to write good object-oriented code is to... learn Haskell.

This applies especially to programming with interfaces. Haskell does have a concept of an interface (but under the name of a typeclass). This, combined with an amazing type system that we are praising all the time in that article, makes programming with interfaces something that doesn't feel awkward and forced, but natural and even necessary. In fact, many functions from Haskell's standard library expect data structures that explicitly satisfy some interface.

These qualities of Haskell are the reason why the language gently pushes you towards interface-centric style of programming and makes it natural to use interfaces a lot.

This stands in opposition to traditional OOP languages, where use of interfaces often feels awkward and unnecessary for the beginners. Haskell's educational value stems from the fact that the language itself encourages you to apply good practices and to craft your code elegantly.

And this knowledge then gets reapplied when you code in other languages, because you have the good practices already ingrained in your mind and you can use them effortlessly, even when writing in a different syntax.

4. You will learn techniques that are directly applicable in other programming languages

Everything that we argued so far might still seem a bit abstract. We said that Haskell will ingrain a certain mindset and coding style in you. But are there some actual, concrete techniques and concepts in Haskell that you will be able to apply in other programming languages?

The answer is of course yes.

We already mentioned a lot - using immutable data structures, curried functions, monads, etc.

Those concepts even just a few years ago where basically unknown to a regular programmer. And now they are being used in basically every popular programming language.

What if I told you that in Haskell there are actually even more interesting concepts and techniques that still didn't find their way into mainstream programming practices? I believe that the concepts that became popular recently are just the tip of the iceberg of valuable stuff that could be brought from Haskell to other languages in a quite straightforward way.

Of course it's pointless to force it and port all the Haskell's standard library and features to other languages. But I am a big proponent of "enhancing" your vocabulary in programming.

There is a high probability that after learning Haskell for a longer time, you will be encountering problems in your everyday coding that are already elegantly solved in Haskell. This could be basically anything. Dealing with null values and exceptions neatly. Writing powerful generators and iterators. Text parsers that are actually easy to code and debug. Dealing with concurrency in a manageable way. Making code that is lazily evaluated in order to save some memory and computing power.

All those things (and more!) are solved in Haskell in very interesting ways, sometimes very different from what you would find in other languages. When you encounter a problem that is surprisingly difficult to be solved elegantly, after learning Haskell you will be able to simply port one of those solutions to some other language, or just get inspired to come up with our own, creative solution in a functional style.

5. You will learn what is currently cutting edge in programming languages and techniques.

As we said, there are many well established concepts and techniques that can be ported from Haskell. But the possibilities and learning opportunities don't end there!

Haskell is a fully functional language used in real world production systems. But it is also a research language. It is an area of constant experimentation and new inventions. The language itself is cutting edge but Haskell has also a lot of experimental additions and enhancements that push it even further.

This is the stuff that you will probably not actually use at the moment and it might be tough to port those features to other languages (although I might be mistaken!). But the whole point is to be able to look at the horizon of programming landscape and to see what might come to our languages in just a few years.

And obviously many of those experiments will fail completely and never get to mainstream programming, but I still believe that it is extremely worthwhile to study those ideas. They will inspire you to come up with your own stuff and show you that there might be more things possible in programming than you expected. It's all about expanding your imagination and creativity, even when using some more traditional language.

Conclusion

In this article I tried to convince you to learn Haskell as your next programming language.

I am not claiming that it is a perfect language. In fact I wouldn't use it myself for most professional projects.

But I do believe learning it has an incredible educational value, much higher than just learning yet another language like Python or Java. Of course those languages have interesting qualities as well and it's worth learning them. But if you want to learn a language that will teach you the most and will push you to be a better programmer, then Haskell should be definitely your primary choice.

Learning only traditional languages is like being a cook that only knows how to prepare his dishes in one way, for example by frying. Simply looking at how the same dish could be prepared in other ways (by boiling, steam-boiling, grilling...) would help the cook to create more varied and interesting meals.

So don't be a cook that just fries everything. Learn Haskell!

If you enjoyed this article, follow me on Twitter, where I am regularly posting articles on JavaScript and functional programming.

(Cover Photo by Surface on Unsplash)

Top comments (4)

Collapse
 
sara profile image
Sara

Great article! I learned some Haskell last year, at first it was difficult for me to change the way I programmed for years, but after a while I got used to it. I was surprised by how powerful and elegant the language actually is. I never used Haskell again, but it was definitely worth it learning.

Collapse
 
mpodlasin profile image
mpodlasin

Hey Sara!

Haskell is definitely very challenging at the beginning, but I believe it's 100% worth it! :)

Thank you for the comment!

Collapse
 
tichopad profile image
Michael Tichopad

Good stuff. I was just considering learning Haskell to get a better grasp of functional concepts. Quite a few people have discouraged me from doing it, actually! But you've got me convinced, sir!

Collapse
 
mpodlasin profile image
mpodlasin

Definitely go for it! 100% worth it.

I am sad that I don't have more time to do Haskell myself!