Why Haskell? Indeed…

I’ve been fascinated by functional programming languages for at least twenty years, but I must admit, despite my rather academic interest, I’ve simply not found them to be very pleasant or useful. Nevertheless, I keep thinking that I haven’t really dedicated myself to resolving this issue fully, and I keep thinking that someday I should get around it it.

Mark Chu-Carroll, over at the Good Math Bad Math blog has decided to try to write a series which will serve as an intermediate tutorial that might convince you that writing programs in the functional programming language Haskell might just be a good idea. His first installment actually fills me with a sense of uneasy foreboding, because it reuses the tired chestnut that we always see in intro to functional programming languages: namely qsort.

Yes, qsort is easy to write in functional programming languages. But the simple fact is: it’s pretty damned simple to write in any language, particular if the language has some kind of list datatype. Consider this version in Python:

def split(x, l):
        lo, hi = [], []
        for y in l:
            if y < x:
                lo.append(y)
            else:
                hi.append(y)
        return (lo, hi)

def qsort(l):
        if l == []:
                return l
        else:   
                lo, hi = split(l[0], l[1:])
                return qsort(lo) + [l[0]] + qsort(hi)

Is this version harder to understand than the Haskell version? I can’t see how it is. Is it less efficient? Well, it’s hard to say actually, at least from principles. Python doesn’t really guarantee much about the efficiency of recursion, and combined with the its applicative order evaluation, it builds a lot of temporary lists that the Haskell version could avoid. But Haskell isn’t exactly a free ride either. Sometimes lazy evaluation can turn around and bite you as well. But the real point is simply this: that this particular example does really very little to justify Haskell as a choice for programming.

Most books on functional programming suffer from this fault. They are demonstrations of trivial or near trivial programs carefully chosen by people who like functional programming because they demonstrate the conciseness of functional programming. The problem is that these examples are convincing only to the converted: they don’t generate any new converts.

Real programming is messy. Efficiency matters. Clarity matters, but cleverness probably does not matter. Handling exceptions reasonably matters. Code reuse matters.

I’m skeptical, but I am still looking forward to the rest of the series.

[tags]Programming Languages,Haskell[/tags]

2 thoughts on “Why Haskell? Indeed…

  1. Ekalavya

    Like you, I have been interested in functional languages for the last 20+ years. I am torn between Lisp, Haskell and Erlang but do a lot of non-work related coding in Lisp and Python.

    I must say that what your claim that these examples don’t generate any new converts is not quite true. There is a lot to be said when a language like Haskell and OCaml can be used to write very concise code that competes very favorably with C code in compute bound tasks. There are well-known commercial successes from the world of functional programming. Granted the number of such successes is not very large but the future of computing can only advance when we liberate ourselves from the clutches of imperative computational model. To that end, I find these examples very inspiring. The debate about efficiency of functional programing is very similar to the one about high-level programming languages and garbage collection and RDBMS. It will pass. But the need to write clear, succint and maintainable code will continue to exist.

  2. Pingback: Functional programming

Comments are closed.