solving reduce with recursion