Problem 2 solved in Haskell.