Solution for 4 in Haskell.