Completed problem 4 in Haskell