Problem 8 solution in haskell