Prototying Haskell solution. Probably very wrong.