Solve problem #5 in Haskell