Learning Haskel For Fun (and profit?)


I found a little spare time recently, so I started learning Haskell via LearnYouAHaskell.com. I’m learning it specifically because it’s completely foreign to me. I’ve never done any functional programming and I wanted to bend my mind around and see what I could learn from that paradigm. So I asked what I should learn on twitter, and that was the overwhelming recommendation.

It’s been fun so far. I’ve only completed the “starting out” section – the very section of writing code, but already there are several things that I can do in Haskel that are making me see other languages a little differently.

List Comprehensions

They’re basically the same thing as “select” and “map” from Ruby or from Underscore.js in JavaScript. And yes, CoffeeScript supports them directly, too. But they are much more compact and supported directly by the Haskell language instead of as functions in a library or pre-compiler. There’s also more too them than just select/map, but I still don’t quite get it all.

Here’s a list comprehension in Haskell, borrowed from LearnYouAHaskell:

Here’s the same thing in Ruby:

(I know you can do list comprehensions in CoffeeScript, too, but I don’t know CoffeeScript yet.)

Tuples

I never understood the point of tuples in .NET. But after seeing them in Haskell, it is starting to make sense – at least in Haskell.

A tuple is an anonymous type, defined at runtime, where the tuple’s type is built from the types of the individual pieces: Tuple<T1, T2, T3>, as an example in .NET terms. The semantics of a Tuple are that it must be the same number of types in the tuple and the same types of each item in the tuple, to be considered equal from a type perspective.

This means that Tuple<int, string> is the same type as Tuple<int, string> in .NET, but is different from Tuple<string, int>. I think I can see some uses for this to provide anonymous types and equality checks amongst them, in .NET – however, it seems to make more sense to me in Haskell where I can assign a tuple at runtime, building up an object type that never existed before, as an anonymous type.

Here’s a tuple in Haskell:

And the same thing in .NET 4:

Both of these create a tuple of type <int, string> (and both of these are statically typed languages). I’m still not 100% sure I see the point of a tuple in .NET since we have Expando and Dynamic… but there must be a use case for a semantically equivalent, anonymous type in .NET, somewhere. I’ve not run into it, but I know other people use the Tuple type in .NET.

I’m also not sure I can show a tuple in Ruby or JavaScript. Anyone know how to do this, and keep the same semantics of type equality?

Other Great Things, Too

Whether or not I ever use Haskel on a project (highly unlikely), the experience will at least help me see different pieces of the other languages I use, in a new light. If you haven’t done any functional programming, check it out. It’s a mind-bender for sure.

But What Do People Build With Haskell?

No, really… what do people write with Haskell? Is it used in real systems? Where? What types of applications and systems? I’d love to know more about it’s real-world uses.

Rebuilding My Backbone.js Plugins With Modules, SRP and More