[Haskell-beginners] How to remove some duplication from this code?