#52WeeksOfCode Week 6 – Haskell

Week: 6

Language: Haskell

IDE(s): HEAT (The Haskell Educational Advancement Tool), TextWrangler, ghc


Haskell, contrary to what you may think, isn’t actually named after the noted Leave It To Beaver character (though that would have been awesome) but after Haskell Brooks Curry, an American mathematician and logician. (There are two other programming languages named after him, Brooks and Curry. The man is something of a pop star to a certain variety of nerd.)


This week I’m treading in some strange waters. As I’ve said before, I’m not a professional programmer so when I write software I use what languages and tools with which I’m familiar (no point learning a new language when I have real work to do) and which seem suitable for my purpose.

As I’ve begun to dig into Haskell, however, I’m really in some foreign territory. First of all, Haskell is described as a ‘purely functional’ programming language. For the benefit of my non-nerd readers, programming languages are generally defined by how they get things done. My experience thus far has been with imperative languages, where software consists of series of commands that change a program state. As a result this is how I think about writing code and how I describe the activity to non-coders. So to me, this is a program:

let a=6
let b=5
echo $TEST

So I have statements that assign the value 6 to the variable ‘a’ and the value 5 to variable ‘b’. Then I assign the result of adding ‘a’ and ‘b’ together to the variable called TEST. Finally I print out the value contained in TEST.

Haskell, however, is an example of a functional language. One of the best descriptions of functional programming I’ve found compares it to a spreadsheet. Each active cell of a spreadsheet contains an expression and we don’t really care about the order in which these expressions are evaluated. We just assume that this gets done in a way that makes sense. Compare this to the chunk of code I presented earlier. The order in which those statements get executed makes a big difference in the outcome. (Before TEST has a value assigned to it, it doesn’t make sense to print it out, for example.)

Fortunately good online documentation for Haskell is readily available, including a very nice tutorial that lets you try Haskell in your Web browser (though the narrator was a bit perky for a curmudgeon like me). There is also a nice cross-platform Haskell IDE available called HEAT (The Haskell Educational Advancement Tool) which is especially designed for use by students and others learning Haskell.

There’s an old saying about having hammers and things looking like nails. (I may not be quoting that right.) Anyway, what this means more generally is that our tools, both physical and mental, influence how we think about solving problems. Software programs are as much a product of the psychology of their creator as a novel or a painting or a piece of music.

If you, like me, are used to solving problems with traditional imperative languages (C, Java, C++, bash, etc.) then your approach to a problem ends up being very ‘low level’. You break the problem down into simpler and simpler sub-tasks until you get to the level that the language can handle. But Haskell is a higher-level language so tasks that are more abstract can be handled directly. Think of it this way: If I want Java to make me a peanut butter and jelly sandwich, I have to break the task down into tiny steps, such as ‘open the cutlery drawer’, ‘pick up a knife’, ‘close the cutlery drawer’ and so on. But in Haskell I could just pass a list of ingredients [“peanut butter”, “jelly] to  MakeSandwich (with a possible option for bread type).

With that in mind, I have the perfect project in mind for my Haskell experiment. However, as I’m the World’s Slowest Coder (™), I won’t be able to finish it in time for this week’s post so for now I’ll be submitting a simpler task. (I’ll be including the Really Cool Haskell Project as bonus content for you lucky and tasteful readers who purchase the final e-book ‘52 Weeks of Code: The Psychology, Art and Philosophy of Programming’).

While we’re on the subject, one thing I notice as I dig into Haskell is there are a lot of words being written by fans enumerating all of the reasons that the rest of us should be using Haskell. Words like ‘pure’ and ‘guaranteed correctness’ and ‘compact code’ get thrown around a lot. They also describe Haskell as a ‘general purpose programming language’.

I have to disagree.

While it is true that any program you can write in a language like C# you can write in Haskell, that doesn’t mean that you should. There are certain tasks that Haskell, by its very nature, is better at than others.

For example, Haskell is better at abstraction so it’s a good fit for programs that used to be lumped under ‘artificial intelligence’ but are now referred to as machine learning or natural language processing. Haskell also has many qualities that make it work well for concurrent programming (multiple computational tasks running concurrently across several CPUs) so it’s easier to do heavy-duty data processing. But my primary take on Haskell is that it’s a very good language for mathematicians and logicians. I noticed this while I was getting up to speed on the language and looking at samples of code, thinking I’ve seen this somewhere before. Then it hit me – I’ve seen this in my math textbooks. This doesn’t take anything away from using Haskell for whatever software you wish, it’s just a better choice for certain types of problems. Haskell advocates admit that you need to change the way you think about problems in order to fully utilize the power of the language. I submit that this actually means that you need to be able to think mathematically and framing a problem in terms of formal logic.

I decided to write a program that determines if I can transform HELLO to WORLD one character at a time, with each intermediate step bringing a real world. (It’s like the old logic puzzle turning ‘cat’ to ‘dog’ with cat->cot->tot->tog->dog.) The implementation will unfortunately take longer than I have this week so let’s do something simpler. That is, let’s replace each character in HELLO with each character in WORLD, one at a time. In other words, our output should look like:







Not as cool, I know, but it’s doable in the time I have.


02/13/14 – 10:25PM – Okay, maybe not so doable in the time I have. I have to admit that rewiring my brain so I can think in Haskell is seriously kicking my butt. I’m making progress but it doesn’t look like I’ll have working code (I can hear the Haskell fan-boys yelling at me and calling me names…).

But I’m running up against my self-imposed deadline so for now I’m going to post what I’ve got sans code and come back to it later. My apologies….


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s