← Back to Upcase

Haskell implementation of FastOrderLine parser


(Joe Ferris) #1

This is a Haskell reimplementation of the problem posed here: http://forum.thoughtbot.com/t/balancing-negative-numbers-from-text/1700/10

The Ruby version that I’m implementing is here: https://gist.github.com/jferris/b0a7a2debdf54213f164/f1520c0f1f0f3cfaeb67bf787d5ee04f00e8fb63

Here’s the Haskell version:

The size of the code is roughly the same as the size in Ruby, but I think a few of the constructs are pretty clean in Haskell.

I ended up defining a type class (Combinable) for this which I’m sure is already implemented somewhere in the Haskell world in some form, but I couldn’t find it. I’d be particularly interested in knowing if there’s a Category for things that can be reduced by combining some of the elements in some way; I’ve had to solve that problem a few times now.


(Mike Burns) #2

I wonder if MonadPlus would solve your Combinable issue. Or Monoid?

Since this is a parser, would Parsec have helped here?


(Joe Ferris) #3

It definitely seems close to Monoid/MonadPlus, but there isn’t really a zero case - how would I define mempty or mzero?

I originally implemented this using Parsec. It ended up being more verbose, because I couldn’t find a good way to have it parse patterns like F12/05/20’ and return them as a single string. Because of that, I had to part out the individual parts, let bindings, and then recombine them again. Maybe there’s a better way to use Parsec? If so, that would be nice, because Parsec handles the “Error on 1: whatever” logic for you (which I implemented in LineParser).