Problem 6 solved in Haskell.