I'm an OK C/C++ programmer. I find Haskell very intriguing. But it seems to me, that although it's relatively easy to write clean Haskell code, as it mimics math (which I'm very comfortable with) pretty well, it's very hard to write clean code in Haskell that runs fast.
A faster version of quicksort of Haskell is very long and scary, which has no resemblance to the naive but short (two lines), clean and intuitive implementation. The long and scary version of Haskell is actually still much slower than the shorter and simpler C counter part.
Is it because the current Haskell compiler is too dumb or is it just impossible for mortals (other than SJP of course) to write fast Haskell code?
To start building real-life applications with Haskell, you can expect to spend about two to three months working on the ins and outs of the language.
In general, Haskell is a good language to learn first. It's already used by a few universities in their intro sequences. I've found it does a really good job of teaching good style. Your code is naturally modular, simple and self-contained, breeding habits you can take with you to other languages.
Out of mainstream languages, I consider C++ the hardest. Theorem-proving languages (like Agda or Coq) are harder than Haskell conceptually. Haskell isn't a hard language, but it takes time to learn its pattern and libraries (both standard and third-party).
Even among the functional languages Haskell is especially hard to learn. This is due to its terse syntax, abstractness, purity, and its community's love of one letter identifier names. Many of these things give Haskell unique strengths, but they also make it hard to learn.
You ask two different questions: learning and performance.
map
, filter
, and fold
. I did all that with ML but it translated to Haskell very easily. You've observed a performance problem only because you've adapted an algorithm loaded with mutation, which Tony Hoare designed for imperative languages, and tried to translate into Haskell. In Haskell as in any other functional language the expensive operation is allocation. Try writing a merge sort and you'll find it's simple and performs very well.
How do you avoid making similar mistakes in the future? Have a look at Chris Okasaki's book Purely Functional Data Structures. Great book, and it will help you learn the 'functional way of doing things' without giving up performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With