Starting with Haskell

2020-08-07

Haskell has always seemed like the holy grail of programming, the language only the most proficient of programmers can understand, reserved for the elite of the elite. All throughout HackerNews and Reddit you can see people praising it for it’s amazing qualities and talking about the difficulty of getting started learning.

So obviously, I felt the need to try to learn it, and down the rabbit hole I went. In this post I detail my experience and the path I took, and I want to specially talk about what were the hardest points as a beginner, so don’t loose perspective when I do get better at the language.

Disclaimer: This wasn’t the best way to start learning, I don’t recommend you follow this as a guide. Also I’m definitely not an expert at the language now, calling myself intermediate would be being extremely generous. I won’t repeat all the good things Haskell has either, as I haven’t been using it enough to be a good judge.

Previous experience with functional programming

Before starting with Haskell I had no prior experience with a functional language, beyond playing with F# for a bit some years ago.

I did have some experience using a functional approach while coding in other languages, using functions like map, reduce, filter, etc., but not much more.

Project

I usually like to have a project to build while learning something, as it helps you discover how to do the actual things you normally would do, instead of solving problems out of context. I find that this approach can get me up to speed faster while also helping maintain motivation. For that reason, I decided to work on a simple command-line chatbot program, using the Inbrainz Cerebro DB as a knowledge base. This ended up being a good basic project, as it taught me IO and database access.

Installation

Getting Haskell working might be a bit complex, as there are a couple methods of installation. I ended up going with Stack, which has a simple set of instructions to follow to get up and running.

Getting started

With Haskell installed, I proceeded by taking a high overview of the syntax and how to do basic things with Learn X in Y minutes, like declaring variables and calling functions. I also started going through Real World Haskell, but I only ended up reading the first chapter. It looks like a good way to learn the language, but it looked too extensive for what I was looking for. I think I’ll get back to it at some point, as it does cover some more complex topics that it would be nice to learn.

I found this project which I intended to use as reference, but it proved to be too advanced for what I knew at that point, so I ended up not getting ideas from it, although looking back at it now, it does seem like it would have helped.

I started playing around with some small programs with basic IO, like taking a string and printing it back.

Then, I couldn’t resist the siren song anymore and dove headfirst to learn about the infamous monads, so into YouTube I went.

Monads

I had seen the What is a Monad? Computerphile video years ago, but it left me with more questions than answers as I didn’t have a good grasp of functional programming or Haskell. I started my quest by rewatching it, but it still left me with too many unanswered questions. Following that, I watched An Intuitive Introduction to Monads in Under 10 Minutes, which is a video in response to the Computerphile video. After finishing it I had a fleeting feeling of understanding, which quickly vanished.

I continued with Haskell for Imperative Programmers #17 - Monads, at which point I realized that there were still a lot of things I had no idea about, so I went to watch the whole series of videos by Philipp Hagenlocher. They have a low viewer count, but I found them easy to follow and high quality. Really recommended for everyone.

After watching those I had a general overview of the concept and it’s usefulness, which then got cemented by watching Brian Beckman: Don’t fear the Monad, an hour long video with some great points.

I’m not sure if this ended up being useful, although I think it gave me some confidence, knowing that the "hard" concepts are actually quite logical and can be understood with some patience.

After watching so many videos, I started working on a simple implementation of the chatbot.

The next roadblock came while trying to get the DB connection working. I opted to go with Sqlite, as I thought it would make it easier than. I can’t really comment if it’s actually easier, as I haven’t used any other DB type. The biggest issue I faced was installing the correct package and how to actually install it.

Stack and Cabal

Coming from the JS and PHP world, it took me a while to realize that Stack is not a package manager like npm or Composer, so running “stack install ...” doesn’t work like “npm install ...” does. A good article to understand how it actually works is An opinionated guide to Haskell in 2018.

The basic overview is that Stack is not a package manager, it’s a build tool. To add a package to your project you need to add it to the project configuration files, and then build it. Definitely read the article above for a more in depth explanation.

With this knowledge, installing the package was pretty straightforward, and with that out of the way, I could focus on working on the project itself.

You can see the resulting code here. I know enough Haskell to know it’s pretty ugly, but I don’t know enough to be able to make it pretty. I do know that I should have used an actual State monad instead of the weird thing I ended up making, but oh well. If you see some big no-no or something that could have been done in a better way, let me know. And on the contrary, if you see something I’ve done correctly, let me know so I can keep doing it.

Conclusion

I’ve definitely enjoyed learning about Haskell, and I can see why people sing it’s praises everywhere. I think it’s going to be a while before I’m comfortable enough with it to build big projects, although I have my eyes on IHP and I’d like to give it a go sometime soon.

I know there is still a lot of interesting features in the language that I don’t even know about, but what I’ve seen shows a deep care and interest to advance the field of programming.