Haskell implementation of a queue-based "rebinner"