Space Saving Algorithm in Haskell