Haskell implementation of balanced reduce