Monday, September 6, 2010

"Do Records" for Haskell

While working on a parser written with Parsec, I began to feel annoyed at some repetitious code like this:

This pattern is no doubt common in any Parsec grammar which produces a fairly straightforward AST. As demonstrated, each field of the particular AST node being constructed is mentioned three times. This pain can be reduced somewhat by using terse names for the intermediate bindings, but this detracts from the readability of the code.

I created a solution to this problem using Template Haskell. The previous example can then be written like this:

Take a look at how it works.

0 comments:

Post a Comment