Completed problem 6 in Haskell