The Hyperpessimist

The grandest failure.

Seven Languages, Week 7: Haskell

So, this evening I finished the homework of Haskell day 3. Immediately after that I went to grab a beer as a small token of celebration for finishing “Seven Languages in Seven Weeks” successfully (I’d say).

What about Haskell? Well, I like it. Programming in a functional way in Haskell is actually quite fun and straightforward. As for functional languages Haskell is at the top and actually quite what I’d wish Scala to be. From the languages I kinda sorta “speak” Haskell compares best to OCaml and in a number of things favorably.

In specific, I really like the type classes and found working with them extremely pleasant. Also, in some cases the Haskell syntax is less messy, especially when defining types, I never liked the alpha, beta, etc stuff that OCaml definitons include. Other things I liked less, like the indentation. Not exectly because of the indentation itself, but the ugly results:

1
2
3
exploreMaze p = let complete = completePaths p
                    found = complete /= []
                    in if found then (head complete) else exploreMaze (p >>= extendPath)

So, I get punished for giving long names. Possibly this can be circumvented, but as I have quite a lot of stuff to do than learning Haskell properly, let me complain about this.

Another thing was that right in the first homework I ran into problems with the monomorphism restriction, so I had to add a line to the top of my code which disabled the restriction. I don’t really know the exact reasons why it happened but I fear the explanation is not simple. But that leads me to some things I really liked about Haskell.

For once, the REPL of GHC, GHCi is pretty awesome. I’d say it is by far the best from the other ones in the book. It supports readline directly, it has tab-completion for files, starts quite fast and works (almost) like if I were writing code in a file (well, I have to use let to set values, but this is shorter than Prologs assert). The error messages of GHC are usually really cool and sometimes even offer the proper name of the function if I mistype function names. Awesome.

There is a number of handy things that I liked, like the . compose operator and even more the $ application operator. Not entirely surprising since my favorite operator is the thrush-operator -> (sometimes called thread-first operator, common in Clojure), so yeah, I like control-flow-bending operators.

Talking about control-flow, let’s talk about monads. Haha, of course, how can one write a discussion of Haskell without monads? Well, I’ll be smart and won’t write a tutorial on monads just yet, but if you haven’t seen monads before, like me, and feared to learn about them because they were deemed “complicated” rest assured they are not. After I saw my first monad, I was more like “that’s all there is to it?”. For once, the “Seven Languages in Seven Weeks” description was quite good and I picked them up quickly.

The book introduces for the Maybe monad which is quite cool, since it solves exactly a problem that I’ll run into in my bachelor thesis in OCaml soon: propagating faulty state without having to pass the fact that something failed everywhere. After I saw the Maybe monad, I thought it would be cool to have a version of it that can not only pass around the fact that something failed but also what exactly. Turns out, such a thing exists and is called the Error monad. So yeah, the trip to Haskell-land has paid off already.

Generally, I quite liked Haskell and even learned some things on the way. If I were to judge, I’d say that the Haskell chapter has learned me more than the other chapters from the book, which is maybe not that surprising since the topics discussed in the Haskell chapter were the most in-depth ones.