Adding problem 39 in Haskell