Monads are Tedious in Go

submited by
Style Pass
2021-08-23 17:30:06

Most of my personal projects are written in Haskell these days. I’ve heard people say “Haskell is hard” or whatever for a long time, but the reason I write most of my projects in Haskell isn’t because I’m smart and want to do the most impressive smart person thing possible, but because I’m dumb and want better tools to help me understand things more easily and avoid the kind of bugs that dumb people like me write a lot.

On any given work day, I review at least one piece of go code. Go kind of has a similar thing in theory about not having “clever” features that might confuse people. Some of this is really nice, but some of it is tedious. I’m going to get into the latter a bit here.

Much of the code I end up reviewing contains many reimplementations of <$> or >>=, sometimes buggy. These are scary looking things to someone who doesn’t write any Haskell, but they’re so fundamental to what most code is doing that you just absorb them quickly.

This post isn’t meant to be a tutorial on Haskell operators, but <$> is also spelled fmap and basically means “apply this function inside that thing.” e.g., you might apply a function to each element of a list, or to the value inside an optional (think nullable pointer). >>= is the monadic “bind” operator and basically is used to combine monadic actions.

Leave a Comment