Thursday, February 12, 2009

Why Haskell?

I was catching up with a friend recently after some years and I talked about how I now preferred programming in Haskell. He didn't know much about Haskell, so he asked my why. On the spur of the moment I didn't give him a good answer, so that got me thinking afterwards. Why do I prefer Haskell and how do I convey it concisely to a mainstream programmer (e.g. Java)?

I think it is a largely pointless exercise to try and have a useful discussion on the benefits/issues of these languages if you haven't at least had experience in functional programming. I had come from years of mainstream imperative programming, with no functional exposure and it took me significant time and effort to unlearn many things I had internalised and get a basic grip on Haskell. It is a fundamental mind shift. I have observed a number of other people, some very talented developers, go through a similar experience.

So perhaps it is best to offer some personal observations on my own programming experiences, simply to pique curiosity. Hopefully it will motivate further investigation.

  • I am not some super programmer - there are other people far better than me. However it is uncanny how often my Haskell code "just works" once it compiles, compared to my experiences with Java over the years.

  • Generics in Java are useful, but cumbersome and limited. Haskell takes parametric polymorphism (aka Generics) to a whole new level.

  • A Haskell program is made up of functions and values, all of which have a type. These types are expressed more concisely than in Java and they provide valuable clues on recurring patterns that can be "refactored" to reduce duplication.

  • Types are such a useful description for functions/values, that I find myself thinking in terms of assembling types so that they all fit together correctly. It can be a little like putting together conventional rectangular Lego blocks, where you might want a 2x4 thick block, and you know you need a 4x4 flat piece next, etc.

  • When I sit down to right some code in Haskell, I find I tend to think more deeply about what it is I am really expressing. Not sure I can clarify that sufficiently, but I think it may be more concern for underlying structure, relationships and patterns.

Like many things, not all is rosy.

Be warned though. If you make the effort to learn Haskell and become familiar enough to find the abstractions, type system and brevity useful, coming back to Java is an unpleasant experience.

Finally, while writing this I happened across a related interview: Bryan O'Sullivan on the Power of Haskell.

1 comment:

Kristian Domagala said...

Be warned though. ... coming back to Java is an unpleasant experience.

After the last week, I would use the phrase "excruciatingly frustrating experience"!

Java's Generics are so constraining after coming from Haskell/Scala, that I'm finding it extremely awkward to express what I want (if indeed I can).

Then there's the endless type annotations and lack of first-class functions.

I keep asking myself "how on earth did I ever get anything useful done in this language?!" :-|