Solution in Haskell.