An implementation in Haskell.