Why should you learn Haskell? As a JavaScript developer7 min read

JavaScript is the first language I’ve tried to get the taste of the programming world and I stick with this language most of my time. When I go a little bit further, I have encountered some concepts such as imperative and declarative programming also functional, object-oriented or procedural. Thoroughly, with curiosity, I gradually envision what they really are, and I found functional and declarative programming they are another way of thinking and writing code compared with imperative programming. It’s a way (more or less) that helps you write better code with fewer bugs.

Object-Oriented Programming (OOP), on the one hand, is a very popular programming paradigm which is based on an object, inheritance, encapsulation, polymorphism, the basic idea is, changing of an object often not only itself but also others, which is a form of imperative programming. Functional programming is a form of declarative programming, on the other hand, it is another programming paradigm that is based on…mathematics. Simply put, functional programming is a language that focuses on the computation of pure functions, avoiding changing and sharing states and mutating data, function composition. I will write more details about functional programming versus OOP and some concepts related to this in the later articles.

Haskell is not really quite popular when compared with Python, JavaScript, C, C++, Java, C#, etc…However, I see many people assert that Haskell is the best choice if you want to dig into the functional programming world, so I’ve tried it for a couple of weeks (I am not professional Haskeller so the words bellow are just from my personal point of view and from my amateur experience, so it could be a little awkward and not really explicit). Here are some reasons why Haskell is worth learning for JavaScript programmers:

  1. You will approach the new way to do things, which I mean is from OOP to FP, it’s the huge different perspectives!
  2. Declarative programming is pretty concise, you will write less code and so fewer bugs.
  3. Learning Haskell is fun!

This article will address each of these reasons in a little more detail as well as provide a roadmap of resources that you can use to get started learning Haskell.

But first, what is Haskell?

Haskell is a statically typed, purely functional programming language with type inference and lazy evaluation. Haskell was made by some really smart guys (with PhDs).

Learning Functional Programming Concepts in a Functional Language

It is possible to write JavaScript in a functional way with, such as map, filter or reduce. However if you are just getting started learning about functional programming, it’s much harder to develop the discipline to adhere to the principles of functional programming in a language that provides so many escape hatches. There will be pain points when you try to problem solve without side effects or mutable state. It is likely you have been using these things so frequently that you do so without even knowing it.

Haskell will not allow you to make any exceptions in this regard. You will need to learn to rely on higher-order functions and recursion to solve many of the same issues you may have previously resolved with for loops and mutable data. It will be difficult at first, but over time you will learn to think differently. This doesn’t mean you should never mutate state or use for loops when you do write JavaScript, but it does mean you will have a lot more options.

Functional languages like Haskell also support function composition natively, which means you will not need a third-party library like Ramda to do things like curry (functions are curried by default in Haskell), compose, map, bind, or flip. Working in Haskell and working through tutorials on Haskell will help you think of functions as small, modular components that can be arranged and rearranged to meet different needs.

Learning Types in a Strongly-Typed Language

JavaScript is dynamically typed, which means it does all the type checking at run time rather than compile time. When you work in JavaScript a variable can reference a String at one point in your program and a Boolean value later. It will let you write a function intended for numbers and then let you pass it a Boolean value elsewhere in the code. JavaScript is the Wild West when it comes to types. To write JavaScript well you need to have a good mental model of what types are and how they affect the code you write. You can use something like TypeScript to enforce some level of type safety, but even TypeScript offers type “any” as a get out jail free card.

Haskell was designed to be a strongly typed language. It will not only enforce the proper use of types, but it will also teach you about types as you learn to write Haskell code. As a result, when you write functions in JavaScript you will start to think of them in terms of their input type and output types, a useful way of thinking when composing functions. When you design your application state and the functions that interact with it,  you can do so with types in mind so that all of the moving parts line up correctly.

Learning Haskell will also help you write better TypeScript. It’s much easier to “cheat” in TypeScript, and having that as an option can be tempting when you run into a difficult problem. TypeScript is also an imperfect system for creating strict type signatures, however without exposure to a good type system, you won’t be able to identify when a problem is caused by the limitations of TypeScript or simply by your own inexperience with types.

Lastly, having a good understanding of types and type signatures from working in Haskell will help you read additional functional programming literature. Many functional programming resources utilize types and type signatures to explain functional concepts and having that vocabulary along with a familiarity of the Haskell syntax, in general, will make many more resources on functional programming available to you.

Haskell is Fun!

No really, Haskell really is a lot of fun to work with and learn! It has a completely different syntax from JavaScript and it has a lot of fun tools for solving problems efficiently like list comprehension, pattern matching, and guards that make for really fun, new solutions to algorithms you may have already solved in JavaScript. Haskell will frustrate you at times, but it will also surprise and delight you as you discover new aspects of the language.

So how should you get started? Unless you are already comfortable writing JavaScript in a functional way, I would recommend you first learn the basics in a language that you already know. I recommend Professor Frisby’s Mostly Adequate Guide to Functional Programming or this course, both by Brian Lonsdorf and this series of articles on composing software by Eric Elliott. You don’t have to go through all of this material, just the first few chapters and articles.

Once you have a good grasp of the basics, I recommend getting the Haskell Book if you don’t mind spending the money (it’s worth it!) or Learn You a Haskell for Great Good! (the book that I’m currently reading) There is also a two-hour course on Pluralsight that serves as a nice quick overview but it is by no means exhaustive. The real advantage to Haskell Book over the others is that it includes exercises for each new concept which means you aren’t just reading about a topic, you are practicing it as you go. To that end, Code Signal is also a great site for putting your new Haskell skills into practice by solving algorithm problems.

Previous Article
Next Article

Buy Me a Coffee