Solves problem 8 in haskell