Solution to problem 29 in haskell