Week 7: Haskell

October 2nd, 2013

Haskell is the seventh language in the book. It is a pure functional language, so functions have no side effects. This makes it easier to reason about the code.

In contrast to most other languages Haskell is not created by a single person. When in the late 1980s a large number of people were designing and implementing functional languages, a committee formed to work together so they’d be stronger. The original goals of the language were to form a foundation for research that was suitable for teaching and industrial usages.

Haskell exploits a static type system, like Scala. It has strong support to infer types. Its type system is build to clearly express the intent of the code. It can prevent much error handling and argument validation. Its type system is much more powerful than Java’s (and so Scala’s).

Bruce compares Haskell with Spock from Star Trek. Like Spock, Haskell is completely predictable (because of its pure functions), and always logical.

This week of Haskell was my first experience with the language. It took me a little while to get started. On day 2 things got better because the exercises got more difficult. I liked the exercise for generating a lazy list with prime numbers. I used a simple algorithm (Siege of Eratosthenes) that is not really fast but easier to implement than many other algorithms.

On day 3 Bruce introduced monads. In short, using a monad we can compose functions that have certain properties. This is not an accurate explanation of monad, and if you wish to really understand them you will have to invest a bit of time reading through example monads and the explanations. Monads are not specific to Haskell, lots of (partly) functional languages support them. Every other functional language in the book (Scala, Erlang, and Clojure) does.

I enjoyed the exercise on day 3 of representing a maze using Haskell’s type system and solving it using the List monad. The exercise of implementing the List monad in a non-functional language was interesting. I chose Ruby and Io.

My solutions to the most interesting exercises in the book are available in the 7-languages-in-7-weeks repository on my Github profile.

One of the major strengths of Haskell is of course its type system. Personally I prefer duck typing over static typing, but that’s mostly because all major static type systems (C, Java, Scala) force so much redundancy. Haskell changes this by understanding that static typing is a tool that is very useful in some situations while not so much in others. Like I said earlier, the compiler can infer types if you don’t provide them.

Other great strengths of Haskell include its lazy computation strategies, the expressiveness of the language, and the purity of its programming model. Also Haskell is the primary language for teaching functional programming techniques.

A definite weakness of Haskell is the very steep learning curve. To get the most out of the language and fully embrace its strengths you have to understand many functional concepts (monads is one of them). As Haskell is a pure functional language, there’s no cheating in using a few non-pure functions.

Personally I find Haskell a particularly interesting language, one that is important to get a good grasp on. As I am not yet comfortable doing some real work with Haskell yet, I definitely want to learn more about it. I believe that getting more proficient in Haskell will strengthen my understanding of functional techniques.

Finishing this week of Haskell only leaves me to wrap up my final thoughts about the book and learning new programming languages. I’ll leave that for next week.