implement exercises from "FP in Scala" book, chapter 4