This is sort of translation of a Haskell version / Ruby version and done while learning Scala. Work in progress. Code coverage: