I used quite a lot of my evenings last week starting to learn Haskell, the lazy, pure functional programming language, and, by Thursday evening, I had cracked one of the exercises from my chosen text (see below) in only 3 hours. That it would have taken me less than a minute in Python or less than two minutes in C/C++ is irrelevant: I’m learning to program differently. I’m not new to functional languages having been brought in a decade or so ago to dig a major project out of some pretty deep mud. The project had genuine real-time components and had been programmed using a somewhat immature Virtual Synchrony package in two incompatible variants of LISP (which I understand is a short-form of “Lots of Infernal Sodding Parentheses”).
I started my journey into the Haskell world with A Gentle Introduction to Haskell which was certainly not gentle enough. Then I found Hal Daumé III’s Yet Another Haskell Tutorial which suddenly brought it all to life.
The most interesting characteristic from the point of view of a naïf Haskellian is the fuss over input/output. One characteristic of a functional language is that the result of calling the same function with the same arguments twice should be the same. This works well for computational functions: if I call a multiply function twice with the arguments 5 and 8 I would expect to get the answer 40 back at least most of the time. Input functions are slightly different. If I call a keyboard input function twice, the value returned reasonably depends on what the user types. This is WRONG by functional programming standards.
At this point, one might have expected the Haskell designers to say, “That’s a pity. We have to have two types of function: one which always returns the same and one which doesn’t. Let’s use a different notation and get over it.” Instead there appears to have been an astonishing attempt to bring those alien, non-deterministic, functions into the fold. The answer was apparently to sacrifice a sheep while facing due west and align its entrails in a Monoid. Monoids are the unifying principle. Now, I think I understand quite a lot about programming languages (albeit not too much about Haskell) and quite a lot about algebraic structures, including monoids. And one day I’m sure that I will see the connexion between the two in Haskell’s input/output functions.
The other observation I made when browsing Amazon for a good Haskell book was that, in addition to their “People who bought this book also bought X, Y and Z” information, they now have “Of the people who browsed this book, 45% ended up buying X, 23% ended up buying Y”. As an author I’m not sure that I want potential purchasers to know that 87% of the people who browsed my book on WBEM/CIM ended up buying Welsh is Fun! by Y Lolfa. I always used to be puzzled by the statistic that many people who bought my Learning to Fly in Canada also bought the New Testament in modern English. The New Testament I could understand amongst people learning to fly but the need for modern English always puzzled me.
Anyway, here’s my nice little procedure for printing the contents of a list. Firstly in Python (sorry about the indenting):
for item in list:
and then in Haskell:
printList  = return ()
printList (x:xs) = do
…putStrLn (show x)