Solution to problem 3 in Haskell.