It’s a Haskell New Year

Well I’ve been keeping at it, despite the fact that it is somewhat of a slog, learning Haskell that is.

The documentation is somewhat sparse, done in that somewhat haphazard fashion of ‘not-quite complete’ style you get with a lot of Open Source stuff – really makes you appreciate what we have in MSDN, I can tell you.  But I thought I’d share a sort of Hello World style of Haskell Program, although it does do a bit more than that  …

module Main

import Char   

main = putStrLn ( show ( my_map Char.isLower "Hello World" ) )

my_map f [] = []
my_map f (x:xs) = f x : my_map f xs

That’s it.  So what does all this do?  What does it mean?

Lets start with

module Main

This is fairly plain. I’m declaring a module, a unit of code really called Main.  The where refers to what comes next.

import Char   

The import Char means I’m referring to the namespace Char which has a number of functions, well one in fact which I need.

main = putStrLn ( show ( my_map Char.isLower "Hello World" ) )

The next line is the actual main function.  Note that functions or more generally expressions in Haskell begin with lowercase letters whilst types such as Main begin with upper case letters.  This is required by the Haskell compiler.  The main function essentially writes output using the putStrLn to write the output of show, which converts a type into a String, and the input to show is the output of the my_map function.

my_map f [] = []
my_map f (x:xs) = f x : my_map f xs

The my_map function is declared on 2 lines and takes two arguments a function and a list.  Lists are defined in square brackets in Haskell  i.e [].  What the declaration says is that if the second argument is an empty list, then return an empty list (first line). If the list is not empty, then  (x:xs) represents the non-empty list as a list element x appended to the front of an existing list xs.  In this case we apply the function f to the individual element x and append it to the output of my_map f as applied to xs, i.e in a recursive manner.

In essence the function is applied to each member of the list.  In the actual code of main, the function applied is the isLower() function which outputs a Boolean value of True if the character is lower case and False if not.

By running this program in the Glasgow Haskell Compiler Interactive window (GHCi), the following is the resulting output.


So this simple program shows the following facets of Haskell:

1.  The program is in fact one function
2.  You can define functions on separate lines (curried).
3.  You generally define functions (non-trivial) as recursive functions
4.  You can pass functions as arguments
5.  You use Actions to deal with IO Issues (putStrLn is an Action)

I’ve done a few other bits, I’m keeping reading bits over and over again, and found some other documentation. Its proving to be a bit of a rough ride and I’m trying to find something ‘useful’ to do and to produce, which is always the issue with these sorts of things.  Still another post will come soon, and I’ll try to produce something better for you next time, maybe try to interact with another library or something…

Meanwhile, have a great New Year, see you around the community!




What the heck is Haskell?

HaskellLogoThat’s a question I get when first asked "What are you doing at the moment?" and I reply "Learning Haskell".

Let’s go back to April 2007, and Ian Cooper comes along to NxtGenUG to give a great session on Object Orientated Practices. During that session he mentions The Pragmatic Programmer as a good book to read to improve oneself as a software developer. As luck would have it my co-worker, utility ‘king’ and good friend Andy Maggs has a copy.  I borrow it,  read it, well most of it, but that one piece of advice in the book which sticks, really sticks, is "Learn a new language each year".

That seems like an awful lot of work, and I put that thought aside. 

Along comes VBUG Conference for which I do a session on LINQ to XML.  During my research, I have a brief flirtation with monads which brings me into contact with Haskell for the first time. Interested, I see its a pure functional language, but don’t really give it much attention.

Along comes MIXUK, and in the last session Simon Peyton-Jones delivers what I have described elsewhere as a ‘thunderbolt’ of a session on Transactional Memory.  During this session Simon mentions Haskell again and the fact that he is one of it’s architects.

By now, my interest is up but so are my commitments, speaking at several UK conferences including DDD, MIXUK, SQLBITS and VBUG Conference.  Also looming is TechEd 2007 where I would be delivering my XSLT Extreme Session.  So I put everything on hold until post TechEd. 

So November 13th I decide that I will grab that thought from above , learn a new language and its going to be Haskell!  Why?  Well here’s my rationale.  I’m not saying you should learn Haskell, just here is why I’m learning Haskell:

  1. XSLT is my favourite language – it’s functional albeit in the XML domain, so functional programming is not entirely alien.
  2. Functional languages seem to offer a potential for coping better with the multi-core future we’re facing, time will tell if this potential can be realised.
  3. Silverlight is not a technology I can really embrace. I have little or no designer skills.  I can understand and do understand the underlying technology, but I don’t believe can make the most of it, besides there are plenty of guys and gals looking at it. Ditto LINQ.  And I felt I needed to do ‘something completely different’ to quote Monty Python.
  4. I spoke to Don Symesat TechEd, one of the main architects of F#, and he encouraged me to learn Haskell as it would stand me in good stead when learning F# if I decided to learn that.
  5. I plan to put together a session over the next 6 months describing what functional programming is and what pluses it offers developers like myself, plus its pitfalls and drawbacks.

That’s why I’m learning Haskell, but why am I writing about this? It’s that man Ian Cooper again.  At DDD6 Geek Dinner, I told Ian what I was doing and how it was actually all his fault, his response: "Make sure you blog your journey through Haskell". 

So, who am I to argue with a lynchpin of the UK community and great speaker that Ian is.  So this was blog number 1 on Haskell, more to come as I delve further and further into this somewhat different world … Next time we’ll have a chat about getting started with Haskell, and what I did to write my first Haskell program …


Dave Mc