Problem 6 in scala