My Journey into Haskell

I first encountered Haskell in my Programming Languages class at Harvey Mudd. In that class, my experience felt like just a constant cycle of:

1. Write code that doesn't compile
2. Make arbitrary changes until it compiles
3. goto 1

I personally struggled with Haskell a lot, and never felt I understood what was going on. I did okay in the class and learned what I needed to learn, so I didn’t give it much more thought.

In the past year and a half, I’ve been writing iOS apps using ReactiveCocoa (RAC), a functional reactive programming framework for Apple platforms, and it’s been a vehicle that’s slowly reintroducing me to functional programming . Being exposed to ReactiveCocoa as well as the wonderful contributors via talks at conferences and social media has made me want to explore functional programming a lot more. I saw immense power in the abstractions offered by RAC and loved using it in my own projects.

I ended up attending RACDC 2015 (ReactiveCocoa Developers Conference) this summer. At RACDC, I met Benjamin Encz and jumped on the opportunity to teach as an iOS summer instructor at Make School. Seeing so many students go from mostly having only basic programming knowledge to shipping their own apps in 8 weeks over the summer really inspired me to learn more.

At this point, I wanted to gain the seemingly magical powers of functional programming. I recalled many saying Haskell is the source of said power, so I embarked on this journey towards power and enlightenment. But most of all, I just really wanted to understand the burrito jokes.

I’ve tried learning Haskell before, mostly via LYAH (Learn You a Haskell for Great Good). Those adorable illustrations were just irresistible, but I found that I never built any solid understanding through the material there. I would work through the chapters, feeling like I understood it, but realize I didn't when I would try to build small projects on my own later.

It’s so easy to assume that Haskell is just another programming language and that it’ll be just as easy to pick up. This is especially true for someone well-versed in a few other languages, as many software engineers are bound to be. When I wanted to learn a new language, I would work through some examples in the new language and effectively build a mental mapping between the new language and languages I already knew. This approach just did not work at all for me when learning Haskell, and this is what LYAH felt like to me.

Through a functional programming Slack and #haskell-beginners on freenode, I discovered Haskell Programming from First Principles and started reading it on my flight out of SFO when the summer ended.

It’s been 3 months since I purchased the book. Since then, I’ve worked through all the chapters published, and it’s definitely been a journey. I don’t think I could’ve built as solid of a foundation as quickly without this book and its wonderful authors, Chris Allen and Julie Moronuki. I’ve been able to successfully use some Haskell libraries in some of my toy projects, including some that I would’ve found to be too daunting to use (looking at you, lens 😰).

I think the approach taken by the Haskell Programming book is spot on, at the very least for me and people like me. (One of the coauthors learned Haskell as her first programming language, so I do wonder how well the book would work for people new to programming.) The book is not only clear and well-written but also chock-full of exercises that help solidify understanding of very important basic concepts. The book starts from the very basics and opens with a chapter on the Lambda Calculus (No computer required. Really!). It then slowly builds ideas on top of ones learned from the previous chapters, all the way up to Monoid-Functor-Applicative-Monad and beyond. There’s a lot to learn, and it took significant effort for me to build up a new mental model for working with and in Haskell, but the book was the perfect guide for me.

This steady building of understanding from the very basics is what’s missing from much of the beginner-level material for Haskell. This post by Chris Allen explains it well:

Well, let’s take the example of the ScottyT monad in the Scotty web framework, a very simple library for making web apps in Haskell. […] To understand ScottyT, you’d need to understand ReaderT because that’s basically what it is — it’s providing the request information and parameters via that mechanism. So now you need to understand ReaderT, which means needing to understand monad transformers and Reader. Understanding monad transformers requires understanding monads. Understanding Reader requires understanding the functor, applicative, and monad of functions. Understanding monads, applicative, and functor requires understanding higher-kinded types, constructor classes. […] Understanding terms, expressions, functions, and values requires understanding the lambda calculus.

Really, it all has to be built up from the beginning. All the differences from familiar programming languages like C/C++/Java/etc., big and small, contribute to what makes Haskell different and special.

In fact, I don’t think Haskell is a particularly hard or complex language. I feel the perceived difficulty is just from how different it is from the languages people are most familiar with these days. I don’t believe there are any shortcuts to learning Haskell. But once I understood the basic mechanics of the language, I found it to be extremely consistent and easy to understand.

So did I gain mystical powers? I don’t know. Maybe I did but haven’t noticed, or the powers have been demystified for me. At the very least, I now understand the burrito jokes. 🌯