functional solution for set2 exercise2