Archive for December, 2007

Learning Haskell with ProjectEuler

Tuesday, December 18th, 2007

Over the past few months, I’ve been spending some time writing code in Haskell. I could devote quite a bit of text to how much I enjoy programming in this language. Suffice it to say, every good thing you’ve ever heard about Haskell is true.

You know, probably.

What this post is really about, though, is Haskell applied to a website on which I’ve been spending some time lately: http://projecteuler.net/. The site offers a collection of mathematical problems that are meant to be solved by writing some code.

If you’re just starting with Haskell (but are an otherwise experienced programmer), this site is a fantastic source of practice. Haskell lends itself particularly well to mathy things like this, and the problems build in complexity as you go along, forcing your code to become increasingly sophisticated.

So, the first problem is pretty much a one-liner, but by the time you get through even the first ten, you’ve started using folds, cool recursions, and interesting function compositions. Many problems force you to do a lot of thinking about complexity and performance, which can be a little tricky in Functional Land if you’ve just arrived from Imperative-ville.

And if you put in some effort to maximize the elegance of your solution code, there is a lot you are likely to learn. You may even find that some functional techniques you used to think impossibly clever start feeling more like second nature. And hey, isn’t that the process you went through to learn imperative languages anyway?

If you are already familiar with Haskell, well… you can still get a lot of enjoyment by thinking smug thoughts about how your three line solution would have looked in most other languages…