Saturday, April 13, 2013

Haskell

I want Haskell to be my primary language for new projects.

It isn't currently, and I don't think it will be for a while. This is due to difficulties I have with the libraries I need to get things done. Yesod is wonderful, but I don't fully grok it yet. I can almost always get web-development done faster in another language. A deep understanding is often required to make progress, this takes time. But still, I love it.

I feel that I'm on the edge of programming nirvana, with just a small deficit in my understanding preventing me from leveraging this language in order to be the most productive I've ever been. So... Why?


What is it about this language that keeps me coming back?



I first played with Haskell in first-year com-sci on my own time as I had heard that it was a really cool piece of technology. It was around the time that I was experimenting with all sorts of .... languages; From  Python to Prolog, Scheme to SML, BASIC to Brainfuck. I gave Haskell an ernest shot but I found it incomprehensible and impenetrable. It was mostly my own fault. When trying a new language there are two main ways to go about it -


  1. Take what you know from elsewhere and see how it fits, or
  2. Drink the Cool-aid


Unfortunately for me I tried the first option, one which Haskell doesn't lend itself to very readily. I would attempt things that I would often do in other languages, such as making heterogeneous lists, class-hierarchies, singletons, etc. While Haskell can do all of these things, they are usually used as intermediate techniques and not brought to bear on simple problems. So I looked at the code I wrote and thought - What is this ugly crap? Why does it have to be this complicated? This isn't intuitive at all.

I gave Haskell a break for the next two years.


Haskell Again


My next attempt at learning Haskell was to work through the Project Euler [2] problems. I blazed through the first 50 of these mostly by using brute-force solutions that were more than fast enough given Haskell's native speed. This time I started to understand the Haskell way of getting things done.

I've subsequently published about 35 Haskell repositories to Github [3] and contributed back to around 4 well known projects.


Why do I think Haskell's ideas will be part of the future of programming?



Haskell features several concepts that are just too good to ignore.


Static types


Haskell's type-system is second to none many research-languages, but is very good none-the less. After using Java, C, and C++ extensively in undergraduate studies, Ruby felt like programming nirvana to me and so I attributed this to the power of dynamic type-systems. My bad. In actual fact many languages have very primitive and prohibitively cumbersome type-systems. Haskell's is the complete opposite. Powerful, Terse, Expressive, and if needed, implicit.

Many fans of dynamic languages say that you don't gain anything from a static type-system as you should be unit testing your code. I don't disagree that tests are a very valuable tool, but types and tests do not fulfil the same role, although there is some overlap. A good design of your program's types will negate the need for many tests, and the ones that you do need to write will be assisted by having a strong static type system to help generate values, and guide the design of your tests.

I have found that my workflow is somewhat different when writing Haskell. I engage heavily in Type-Driven-Development. Writing out my program as a data-transformation pipeline and ensuring that the types of the segments line up, leaving the bodies undefined initially, filling in the pieces in the most-obvious fashion, then engaging in equational reasoning to refactor my source into a better form.


A good REPL


It's true that Haskell's REPL isn't as good as a good lisp's, but it is certainly better than the average REPL. The ability to test the types of ad-hoc values, browse module, and jump into an editor where required really speeds up your workflow. Also, the ability to script the REPL comes in very handy for writing Vim scripts, etc.


Laziness [4]


This is a contentious point, but the ability to not have to worry about the order of my source-code really frees me up in terms of concentrating on the obvious first. When I wrote a project in Clojure it was jarring having to declare my functions or write them in their dependent order. Even most OO languages are more flexible than this tanks to functions being grouped into classes.

Haskell is certainly the laziest language I've ever used, with perhaps the exception of Prolog. The canonical fibonacci definition of (fibs = 1:1:zipWith (+) fibs (tail fibs)) goes a ways to demonstrate the depth that this laziness descends to.


Good syntax


Some people consider syntax to be largely irrelevant to the quality of a programming language, I am not one of these people. I'm from that camp that the more often you use something, the better quality that thing should be. That's why I bought a good razor to shave with, etc. If I'm going to be battling with syntax every time I define a function, declare a type, construct a data-structure, meta-program, etc. I will GET PISSED OFF. Full stop. So far, Haskell's syntax has not pissed me off. This is a very good thing. GHC's extensions also go a long way to help with many sticking points: Just yesterday I discovered the View Patterns [5] extension that generalises pattern matching to abstract data. You could try a new extension every week for a year and still have some left over.


Speed


Haskell isn't the fastest language in the world, but it is fast. And you can tweak it to be very fast. And if you can't do that, then the FFI is first-rate. So far I haven't had any issues with speed, although obviously you're going to be mostly limited by the algorithm you use. Thankfully, in Haskell, refactoring your algorithms is pure joy!


A great compiler


GHC is possibly the world's most advanced compiler. Well, I jest, but it's up there with LLVM that's for sure. The optimisations that it performs are mostly high-level at this stage, but they can be quite amazing.


A great community


I have not yet come across any abrasive personalities from the Haskell community. Also, nobody is beyond reach. You can literally talk to anyone in the community without fear of rebuke (provided you're not totally out of line). There are great mailing-lists, irc-channels, Google Plus communities, etc. Everyone is super friendly and super helpful [6].

The only issue you will come across is the tendency of some people to offer up more general solutions than you might be looking for. This is usually a kind of humor, however, looking into the concepts alluded to is rarely a waste of time.


Links


  1. Haskell
  2. Project Euler
  3. Sordina's Github
  4. Lazy Evaluation
  5. View Patterns
  6. Trolling Haskell


No comments:

Post a Comment