Rearrange haskell solution to p9.