Question

I kind of understand recursion. We start on one method and it calls itself until it reaches its base case. Please help me understand how this code works. I know we are popping off an object off stack each time it is called we are returning a double at the end when the base case is reached.Is stack being modified each time it is called? For example: 3 (Enter) 5 (Enter) * would obviously equal 15. However based on the method when I look at it I follow that it goes into multiply if statement sends stack (3,5) returns 5 then on the second call sends(3,5) and returns 5 again? Why is the second one being sent stack(3) instead of (3,5)?

   + (double)popOperandOffProgramStack:(NSMutableArray *)stack
   {
       double result = 0;

       id topOfStack = [stack lastObject];
       if (topOfStack) [stack removeLastObject];

       if ([topOfStack isKindOfClass:[NSNumber class]])
       {
           result = [topOfStack doubleValue];
       }
       else if ([topOfStack isKindOfClass:[NSString class]])
       {
           NSString *operation = topOfStack;
           if ([operation isEqualToString:@"+"]) {
               result = [self popOperandOffProgramStack:stack] +
                        [self popOperandOffProgramStack:stack];
           } else if ([@"*" isEqualToString:operation]) {
               result = [self popOperandOffProgramStack:stack] *
                        [self popOperandOffProgramStack:stack];
           } else if ([operation isEqualToString:@"-"]) {
               double subtrahend = [self popOperandOffProgramStack:stack];
               result = [self popOperandOffProgramStack:stack] - subtrahend;
           } else if ([operation isEqualToString:@"/"]) {
               double divisor = [self popOperandOffProgramStack:stack];
               if (divisor) result = [self popOperandOffProgramStack:stack] / divisor;
           }
       }

       return result;
   }
Was it helpful?

Solution

You write:

I know we are popping off an object off stack each time it is called we are returning a double at the end when the base case is reached.Is stack being modified each time it is called?

So you know an object is popped off the stack, which means the stack changes. So why would you think the stack as not modified?

Are you maybe confusing the contents of the variable stack, with the object it references? The contents of the variable stack do not change - that contents is a reference to an object (or type NSMutableArray). However the internal state of the referenced object change, each time removeLastObject is called.

Think of it like a house - which has a street address (the reference to the object), and some people in it (the internal state of the object) - as people enter and leave the house (items pushed and popped from the stack) then the internal state keeps changing, but the street address of the house never changes.

HTH.

OTHER TIPS

it goes into multiply if statement sends stack (3,5) returns 5 then on the second call sends(3,5) and returns 5 again?

No. After the first call the stack is reduced. On the second call it sends only (3). Relevant code modifying the stack:

if (topOfStack) [stack removeLastObject];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top