Solve problem 5 in haskell