@niftyn8's haskell solution