You are here

Oh Haskell

I built some code recently to generate SVG that could be transformed into PDF and then fed to a laser cutter along with a bamboo sheet to get markers for the Glass Plate Game. (This is why I cared about X DPI: I wanted to see what the markers would look like "actual size".)

I wrote the SVG generation code in Python 3, because it seemed reasonably easy and portable. After giving up on the Cairo binding because it didn't seem to have any reasonable way to put physical units in the SVG, I decided to just generate the SVG "by hand". It took some time to get right, but in the end I had a pretty nice program. Noting that PDXFunc was meeting tomorrow night, today I decided to try a Haskell rewrite for comparison purposes… I dug through Hackage to see if there was anything helpful for generating SVG. The only thing that looked promising was a package called blaze-svg, whose primary offering was fast SVG generation. I didn't care about speed, but I was curious about the tool, so I gave a try.

Whew. Four hours later, I had something that more or less worked, and another hour after that I had something I could ship. The fundamental underlying monadness of blaze-svg made it difficult to figure out, as well as its interconnection with the blaze framework on which it was based. As I write this, I have discovered and pushed one more code improvement.

The bottom line is that I find the Python much easier to read, write and understand than the Haskell for this problem. To be fair, the comparison I am doing is not square: I should be generating the SVG directly in the Haskell code as I do with Python. I might try that. But some problems arise more from the limitations of functional programming. For example, in Python I can number each token while doing a 2D layout by just incrementing a token counter. In Haskell, I ended up using the IX class to get an index from an (x, y) tuple. This was hard to get right, and led to various bugs that had to be fixed.

There are times when I'm way happier with Haskell. For a problem like this one, that is small, simple, non-critical and IO-heavy, I think I might prefer Python. Nickle would be even better, of course, but I want to ship something easy for other people to use. (This would be another reason to take the blaze-svg code out of the Haskell also.) So here I am. Fob

Group/Project: