Solution in Haskell