Why I Like Haskell

Some time ago I bought a book on Haskell. Couple days ago I had read enough of it to start playing around with this language.

Damn, I never thought I'd fall in love with a pure functional language in such a short time!

This short article is here to share my impressions and thoughts. Or to put it in simple words, Why I Like Haskell.

First of all, I don't think I'll ever use Haskell in production code. Furthermore, I strongly doubt I'd ever use it in my own hacking projects. Nevertheless, I find this language so mind opening, that I recommend any seasoned programmer to make friends with it. Especially, if no other pure functional language was ever looked at.

Random reasons why I like it:

  • I finally got a taste of declarative programming. Before that, I only saw it in a lame university Prolog course, and from that, I thought declarative programming is a bad idea. Now I don't think so.

    Example1:

            fibonacci 1 = 1
            fibonacci 2 = 1
            fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
            

    Example2:

            -- TODO: think of a more meaningful example for this notation
            comparison x y | x < y = "First argument is lesser"
                           | x > y = "First argument is greater"
                           | otherwise = "The arguments are equal"
            
  • Lazy evaluation rocks.

    How do you make a list of n integers in Python? range (n) produces that. How do you do the same in Haskell? You write [0..n]. Literally. You can even define step this way: [1, 3 .. n]. What does it have to do with lazy evaluation? You can omit the upper bound of the range! [0, 2 ..] Will give you never-ending list of even numbers! And no, you're not going to run out of memory, because lazy evaluation will do the smart part for you. The list will only expand as much as your code actually requests. Beyond that, there's only a rule how to generate the next element. Neat-o.

  • Mathematical notation rocks.

    Example1:

            squareRoots a b c = ((-b + d) / twice_a, (-b - d) / twice_a)
                                where d       = sqrt (b * b - 4 * a * c)
                                      twice_a = 2 * a
            

    Example2:

            countLowerChars s = length [c | c <- s, isLower c]
            -- I don't show the isLower function here for terseness
            
  • The language not only originated at, but is also designed to solve problems. "Solve" as in mathematics -- "solve equation", not as in the usual programmer's understanding of "solving" meaning "writing code that does something useful". You just keep reasoning about the problem (in Haskell of course) until the problem gets solved instead of reaching a point where you have some pile of code that gets the result by accident.

    This way of thinking about software suggests some insights that I've never seen suggested by any other language.

  • This is not really a big deal, but lambdas in Haskell are written in the most brief way I've ever seen. And they, again, are very close to mathematical notation.

    Example:

            pow2s n = map (\x -> 2 ^ x) [0 .. n]
            
  • Builtin integral type of unlimited precision is really comfy. Not as if it were Haskell-specific, but I can't resist mentioning it :-).

I am sure this page is going to expand as I learn more and more of Haskell. I am not doing it systematically, neither have I goals to do so in the future, but from time to time I open up that book and almost every page I read makes some interesting insights.

Update (2008-06-21):

Here is a similar in spirit post, sharing first impressions on Haskell. Linking to it in order to keep DRY.

Stay tuned!

-rtfb

Valid HTML 4.01 Strict