Solve #40 in Haskell.