Archive for March, 2007

Parsers, Parsec and Haskell

Sunday, March 11th, 2007

I’ve been having a really odd mental block over the last week or so. For some reason I could not for the life of me figure out how to write parsers. I wrote a really trivial RPN parser a while ago, but had never really done anything more complicated. I’d tried a couple parser generators and just completely blanked on them.

So, this morning I applied the classic mantra (which I just made up now): There’s no problem that can’t be made easier by making it harder. I’ve been meaning to give Haskell a proper try for a while, and I’d heard good things about Parsec, so I thought I’d have a go at writing a parser in Haskell. Never mind trivia such as this would probably entail about twice as much Haskell code as I’d previously written. It would be a good learning experience. :-)

After a few hours spent digging through examples I eventually got to grips with it and put together a simple JSON parser. It seems to work reasonably well, and now that I actually understand the basics of parsec it’s reasonably simple.

All told I’m fairly impressed. I’ve been uninspired by Haskell in the past, but I didn’t find any of the things I expected to bother me to be an issue. The purity was never going to be a problem. I write very pure ML under normal circumstances. I went months of programming ML before I ever discovered that ML even had reference types. Monads aren’t particularly scary - they require a bit of thought to understand, but very little to use. The syntax is a little odd, but not that much. Certainly not to the point where it would put me off using the language.

In fact, it was generally a rather nice experience. I’m planning to write a toy compiler to play around with, and I’ll almost certainly end up implementing it in Haskell.

Build scripts

Saturday, March 3rd, 2007

There seems to be some general perception, at least among Java programmers. that build scripts are scary and/or too tedious to write for yourself. I conjecture that there are two classes of people who feel this - those who have been put off by make, and those who have been put off by overuse of IDEs and feel that anything the IDE controls so completely must be hard work to do by hand.

I was certainly in the latter camp for a while, and even once I realised that build scripts weren’t scary I still thought they were hard work. I finally got around to writing my first ant script earlier (and, for bonus points, my first ant awful hack), and they’re really surprisingly easy. There’s almost nothing to it.

I really don’t know why we’re so dependent on the IDE for our build process at work. Life would be a lot simpler if we weren’t.