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.