Improved the haskell implementation of the first dm.