A Taste of Haskell

Back in the Oregon Convention Center. Pretty handy that two of the biggest open source get-togethers are a short ride up the Esplanade. Con-way even ponied-up for the OSCON tutorials.I went to the Haskell talk because all the cool kids are were talking about it. (Gerry tells me that they are on to Erlang now.) Anyway, Simon Peyton-Jones from Microsoft Research (yes, that’s right) gave an energized talk to all of us later-to-the-party programmers.Things didn’t look too promising at the start, though. We had some presenter equipment set-up hijinks: “Any Ubuntu experts here? How do you increase the font size in Emacs?” Once that was all squared-away, I noticed that the presenter was barefoot.Audience poll! 30% have written a Haskell program.OK, what is Haskell? A functional, lazy, (really) strongly-typed, compiled, general purpose language. It’s about value, not state … what? Yeah, I know, that doesn’t make much sense. It did make sense to me about three hours later, but it’s beyond my ability to enlighten you via this blog. Sorry! Try http://haskell.org/haskellwiki/Haskell or http://en.wikipedia.org/wiki/Haskell_%28programming_language%29.It’s a research language, and the presenter is one of the inventors. He defines a successful research language as one that dies a slow death rather than an immediate death, so Haskell is successful.We’re using xmonad as an example of a Haskell application. It’s an X11 window manager. It’s not exactly Enlightenment. And the first big diagram is a state machine! Uh, isn’t Haskell about values, not state? We find out later that since we’re dealing with I/O — the outside world — we can’t keep everything pure. Turns out that, for quite a while, Haskell was a nifty language that couldn’t do any I/O, which rather limited its practical value.OK, here’s a code sample. The syntax seems reasonable, but everything looks verbose to me after Ruby. Functions are defined by pattern matching. In some sense, they are a “collection of equations.” The code below basically says: for an empty list, return an empty list; for a list of one, return a list of one followed by an empty list; for more than one, return the same list with the first and second elements swapped.swap :: w -> w -> wswap [] = []swap (w : []) = w : []swap (w1 : w2 : ws) = w2 : w1 : wsThis is pretty cool. Pattern matching forces us to think of all cases. The presenter didn’t mention this, but I wonder if they are driving at the goal of mathematically-provable programs. It reminds me of Eiffel and its design by contract.Haskell has a lot of the things you’d exepect. There’s a library called Test.QuickCheck that passes in 100 (random?) values into each of your functions to ensure that you’ve covered all the cases. (As you might imagine, functions are easy to test.) There’s a documentation generator, a package repository and general-purpose libraries.We dived a whole level deeper into Haskell, but I’m not sure I can do it justice. So, the big picture?

  • No side effects at all
  • Purity makes the interface explicit
  • Types are everywhere

Haskell is trying to be useful and safe (safe == no side effects). It is is very safe, but needs to become more useful. From Haskell’s perspective, Java, C#, and Ruby are very useful languages that are becoming safer. Apparently Haskell’s not useless, as there’s a company in Portland that employs twenty Haskell coders and would like to hire some more.Did the presenter just say, “The nettle must be grasped?”

Leave a Reply

You must be logged in to post a comment.

gears