Software Musings

Currying Favour

Posted on: 04/02/2007

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:
…print item

and then in Haskell:

printList [] = return ()
printList (x:xs) = do
…putStrLn (show x)
…printList xs

Advertisements

4 Responses to "Currying Favour"

I’m learning Haskell, too:

printList a = mapM_ print a

And by the way, I have now found an even better Haskell text: “Programming in Haskell” by Graham Hutton (ISBN 0-521-69269-5). A very easy read.

You’ll doubtless be astonished to learn that the plug was eventually pulled on the project comprising (comprised of?) “genuine real-time components and had been programmed using a somewhat immature Virtual Synchrony package in two incompatible variants of LISP” as late as 2003! It turned into a nice little earner for a certain software engineer…

Ijon-

For a post called “Currying Favor,” the least we can do is curry that function:

printList = mapM_ print

Enjoy learning Haskell!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

February 2007
M T W T F S S
« Jan   Mar »
 1234
567891011
12131415161718
19202122232425
262728  

Disclaimer

The author of this blog used to be an employee of Nortel. Even when he worked for Nortel the views expressed in the blog did not represent the views of Nortel. Now that he has left, the chances are even smaller that his views match those of Nortel.
%d bloggers like this: