Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long does it take for you to be comfortable with Haskell?

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?

like image 272
obecalp Avatar asked Dec 18 '08 07:12

obecalp


People also ask

How long will it take to learn Haskell?

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.

Is Haskell beginner friendly?

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.

Is Haskell the hardest language?

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).

Why is Haskell so hard?

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.


1 Answers

You ask two different questions: learning and performance.

  • It took me about a month to become comfortable with functional programming using recursion, pattern matching, map, filter, and fold. I did all that with ML but it translated to Haskell very easily.
  • It took me two or three years to wrap my head around monads, but that's because I read the wrong stuff. I think there are better tutorials now. But if you're beginning, avoid monads for a while.
  • It took me several months to get good at creating new type classes, but using the existing ones was easy.
  • I'm still not sure I have the hang of lazy evaluation. But I love Haskell's purity and tend to treat lazy evaluation as an unhappy accident that only a few people (like John Hughes) know how to exploit.

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.

like image 67
Norman Ramsey Avatar answered Sep 28 '22 16:09

Norman Ramsey