updated problem 9 to use foldr