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.