What’s special about Haskell?

Ani Ravi,HaskellProgrammingLanguages

I started programming by learning Java. After taking a break from learning programming, I jumped into web development by learning JavaScript. Unsatisfied, I eventually moved to TypeScript. I’ve seen a little bit of what a few different languages have to offer and have dabbled in some others not mentioned here.

So what’s special about Haskell?

I didn’t know anything about functional programming until about a year and a half ago — I had no idea that the things I liked most about programming actually came from functional programming. I was later introduced to ReasonML and eventually found Haskell.

Haskell seemed like a mysterious beast at first. I didn’t understand the syntax at all, and it looked different from any other language I had used. It seemed hard to read. Looking back, the things I found so confusing make so much sense, and I only found it so hard to read even basic code simply because I was unfamiliar, not because there was anything inherently difficult to understand.

One effect of learning Haskell is that it completely changes the way you think about writing programs, coming from a background like mine. This also means that there will be a greater learning curve, and it’s unlikely you’ll make the leap unless you’re forced to or you’re interested in learning it in the first place. I have had a hard time convincing people as to why you should learn it, also due to the fact I am still a complete beginner.

The most novel things that blew my mind coming from other languages were:

  1. Way more sophisticated type inference. It repeatedly demonstrates to you that types are not just for error checking as is a common misconception and reductive view of the real power of type systems. Types are meant to be composable specifications to help you model your programs. Good types make refactoring fearless, as your application grows in size and you will be making changes often. The more of your domain that you can model in the type system, the more the compiler can help you make sure you’re doing what you told it you would. It’s a conversation between you and your compiler.
  2. Ability for the type system to track things that are side effects vs pure functions. A pure function is a function that only does what it needs to do with its inputs and returns back the same output given the same input (like in math!). For example, an add function that adds two numbers will take two numbers and return the addition of those two numbers, nothing else. If it did a side effect, it might do something like try to log to the console or send an email before adding the two numbers. In Haskell, you cannot just send an email inside of a pure function without changing the type of the entire function to indicate a side effect will be performed. By isolating the pure from the impure, it’s easier for you, the programmer, to understand what your code is doing (or intending to do).
  3. Concise code, and robust abstractions (aka tools for you, the programmer to use) built on mathematical foundations. Haskell gives you a new set of building blocks and primitives upon which to build your programs, many of which are not the same as the ones you may be very comfortable using in other languages, such as for loops. Haskell has its warts, but many of the ideas you’ll encounter even simply by starting to learn the language have been built with rigor and decades of research.

What’s most important about number 3 is that any living codebase will increase in complexity, even if it doesn’t grow in size (aka lines of code). To be able to manage this well, you need the right tools.

Something I find to be a big problem in programming is we’ve taught programmers to expect immediate results and immediate positive feedback from starting a new project.

“You mean I can’t spin up a production project in 5 minutes??”

Haskell is one of those things you learn because you are willing to pay the upfront cost to have an easier time managing complexity down the road. Just because something looks easy right now doesn’t mean it will be easy as your project grows. There is always a cost. Haskell pushes more of the costs upfront (making you reason about types, new things to learn) instead of giving you the easiest possible onboarding and leaving you to think that the tools are powerful enough to grow with you as your project grows. We have a job as programmers to use the best tools at our disposal to build the most reliable programs.

If you are looking for a batteries-included dive into Haskell web development though, check out IHP!

Nothing I’ve mentioned thus far is what I’ve found most compelling about this language. So what’s special about it?

Haskell is the first language I’ve learned that makes you reason about what code means, not what it does. Whenever I learned how to code, I always wanted to do some set of actions to accomplish a particular goal. When I wanted to learn how to put stuff in a database, I learned the basics of whatever tool I needed to insert data into a database. When I needed to render something on the UI, I learned a frontend framework to make UIs.

For the first time in my life, a language has made me interested in programming in itself — to reason about code at a higher-level and learn how to use the vast array of tools available in Haskell to express your constraints and domain. Things I would’ve never been interested in learning before learning Haskell (e.g. something like category theory) are now suddenly interesting because it underlies a rich history of ideas that you can use to write concise, robust, maintainable, understandable, and composable code. Composable meaning that you can build one lego block of your program at a time, and put them together nicely with the tools afforded to you by the language. The building blocks can look simple at first, just like they often can in math, but how you put them together makes all the difference.

I’m still a beginner at Haskell. I’ve barely touched the surface, and am just dipping my toes into writing some actual programs with it outside of my day job. I worry I will never be able to enjoy “regular” programming again, however, I feel like I’m already getting to that point. If you are interested at all in expanding your mind as a programmer, and being able to learn lots of good ideas to improve your programming skills overall, learn some Haskell. It turns out a lot of the cool things you learn in Haskell help you write better code in most languages you will use nowadays because you aren’t only learning a new language, but also learning new ways to think. Haskell is often at the edge of programming language research — the capabilities you see in Haskell are eventually picked up by more mainstream languages.

Ani Ravi