Exercise for Functional Programming in Scala