Why Haskell ?

What is Haskell ?

Haskell is an advanced, next generation programming language, originally developed by universities for programming language research but increasingly finding it's way into commercial environments. It's particularly suited to highly concurrent and parallelised software where security, reliability and high performance are important; for example network servers, finance and web applications.

Haskell a is pure-functional language, which means it operates in a different paradigm than current mainstream programming languages. It implements many advanced features such as lazy evaluation, type inference, automatic memoisation, software transactional memory and green threads. More about those later.

Haskell is used by a number of forward thinking organisations including Standard Chartered Bank, Facebook, Bank Of America, Google, Galois, Intel and many others. 

Whilst Haskell has not yet hit the mainstream there is a small army of frustrated Haskell programmers desperate to ditch existing laborious, clumsy (sorry) OOP languages for the new world of terse, type safe, immutable functional programming.

Advantages of Haskell ?

1. Terseness - that is, Haskell programs are very short. Haskell generally requires about 1/10th the amount of code to express a given program than current mainstream languages. Haskell does this whilst increasing readability and reducing cognitive load on programmers. This may seem like a bold, even ludicrous claim, but it's true. When you're steeped in current programming languages you just don't appreciate how much of what you type is just noise. It turns out that the noise can be stripped away to leave the pure meaning behind your intentions.

2. Composability  - Haskell programs are structured differently than OOP programs. It takes some getting used to but it's simpler than OOP without losing any of the power. Haskell programs are constructed from small functions that do nothing but take specified inputs and return specified outputs. This sounds simple and it is but it has some incredible consequences. Functions in Haskell have been likened to lego bricks - except unlike lego it's possible to take the same brick and re-use it over and over again in ways that are either not practical or would be too clumsy in other language.

3. Refactorability - Refactoring in Haskell is in a different league to other languages. I'm going to struggle to explain the joy of re-factoring Haskell code; it needs to be experienced to be appreciated. For one thing, ugly and redundant code tends to stand out like a sore thumb highlighting the parts that can and should be re-factored. For another, because functions cannot have side effects it becomes easy and safe to combine and replace sections of code, further reducing the amount of code to improve readability and efficiency.  

4. Compiler Assistance - Any compiled, statically typed language (like C#, Java, C, C++ etc) will be type-safe; ie will prevent the programmer from making silly errors such as spelling mistakes or trying to use an object or method in the wrong way. Haskell's compiler though is somehow greater than the sum of its parts. Haskell development is like having a "conversation with the compiler". The effect is that the compiler really helps with solving problems. Given an idea for a program, I can sketch out the solution quite carelessly and have the compiler iteratively tell me what I've forgotten about until I have a working program. The program will often be a mess but then the easy refactoring kicks in to make the program elegant whilst the compiler "has my back" to ensure my program still works.

5. Parallelism - Haskell enforces separation between "pure" and "impure" functions. A pure function is one that doesn't have "side effects". This means is that if I call a pure function with the same input parameters I will always get the same answer. An impure function is one that might give a different answer from the same inputs. A call to a database would be impure because the result will depend on the contents of the database which may have changed since the previous  call to the same function. This enforced separation between pure and impure code has a number of important consequences, not least of which is that it means the compiler is able to automatically and reliably parallelise code to take full advantage of multi-core processors. This is ability is going to become critical as Moores Law hits diminishing returns and processors adopt ever more massively multi-core architectures.

6. "If it compiles, it works" - This is one of phrases that drew me to Haskell, mainly because I was sceptical and wanted to reassure myself it was nonsense. In fairness, it isn't absolutely true. It is still possible to have errors at run time but they are an order of magnitude less likely than other statically typed languages (C#, Java etc) and at least 2 orders of magnitude less likely than dynamically typed languages (JavaScript, PHP, Python etc). There are a number of reasons for this, but the main one is that the compiler knows all the input and all the outputs to your program and it forces the programmer to account for every possibility. Haskell uses a branch of Mathematics called Category Theory to essentially prove and enforce that your program is correct.

7. Performance - in many cases performance isn't actually that important however it's definitely a nice to have. Haskell programs have an instantaneous responsiveness that's very satisfying. Of course it's theoretically possible if not usually practical, to achieve higher performance with expert programmers using C or C++ but Haskell is very fast. Beautifully fast enough for my purposes of web application development and personalised content delivery.

8. Lazy Evaluation - OOP programs tend to work by collecting and organising data in a series of steps ending with the desired output. Computations often happen *just in case* a result is needed by a later part of the program and then discarded if not used. OOP programmers try to minimise such wastage, but in order to keep programs manageably simple some waste is inevitable. Haskell programs work by creating chains of computation which are evaluated as the values they produce are consumed. I think of this like a sausage machine (a very complex one!). As the user pulls a sausage out of the end of the machine, one sausage worth of ingredients is automatically drawn through machine. This lazy evaluation allows the compiler to automatically apply impressive performance optimisations and also allows programs to operate on large or even infinite datasets using very low memory footprints.

9. Competitive Advantage - Ultimately there isn't anything you can do with Haskell that you can't do with imperative / OOP languages but the fact is, Haskell does it with less code, less cognitive load, generally less memory usage, easy parallelism and is easier to re-factor and change the code later. If your business relies on creating code for it's profitability, these advantages have potential to make a serious or even extreme contribution to the bottom line. As of 2016, OOP still has advantages in terms of maturity, existing solutions, tooling, mind-share and not least documentation but Haskell is a next generation tool and the balance is continually shifting in it's favour.

Disadvantages of Haskell ?

The main disadvantage of Haskell is how different it is from other languages. If you're a competent programmer in one language you usually can pick up the basics of another language in a few hours by following simple tutorials. Haskell is not like the others. Haskell is overflowing with new concepts - pattern matching, list comprehensions, guards, monads, monoids, functors, applicative functors, the list is endless and overwhelming. To make matters worse, many of the concepts are represented by symbols which are impossible to google, eg <$> $ <- -> | etc.

There are some great Haskell language tutorials out there but the availability of documentation for most libraries and frameworks is very low. In too many cases it's case of working it out yourself from the source code. This can be awkward when as a beginner the source code reads like alien hieroglyphics. One weird aspect of Haskell for me when I was learning is that it's surprisingly easy to see what unfamiliar Haskell code does whilst not being able to comprehend how it could be doing it or how it could be adapted for another purpose.

I've spent a year full time learning Haskell on my own from online tutorials. It's been the most taxing intellectual challenge of my life. There are still things I don't know how to do in Haskell that I would find easy in other languages. It would have been much easier if I'd had a Haskell buddy to ask questions. There is a helpful Haskell IRC channel and support forums but having someone sitting at the next desk could have saved many months of Googling and frustration. It's been an arduous journey but one I'm glad I undertook.

More Information about Haskell

If this article has made you curious to find out more about Haskell, the following are some good resources:

The journey of a 1000 miles begins with a single step. Good luck.  For me, it's been so worth it ! (I'm at about the 350 mile mark, I think)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.