Refactored Haskell solution 1.