Docsity

Understanding Recursion: A Programming Concept, Slides of Advanced Computer Architecture

Partial preview of the text

Download Understanding Recursion: A Programming Concept and more Slides Advanced Computer Architecture in PDF only on Docsity!

Recursion

  • similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually reaching your goal
  • accomplished by a method invoking itself!
    • so, if it invokes itself - then let’s think about that - how do you think it’s going to stop???

Recursion

Iteration

public void pickAll() {

while (nextToABeeper()) pickBeeper();

}

Recursion public void pickAll() { if (! nextToABeeper() return; pickBeeper(); pickAll(); }

This recursive definition goes like this: Are there any beepers left on this corner? No - then we’re done. Yes - then pick one beeper and then pickAll. can you write this another way?

You Try!

  • write a recursive method called goToSouthWall
    • precondition: bot is already facing south - there are no walls in the way
    • postcondition: bot is at the south wall still facing south - it is still on the same avenue
  • What are preconditions & postconditions?

the things that need to be true in order for the method to perform its task

  • the contract between the client and the method

what will be true after the method has finished executing - only guaranteed if the preconditions are met

Writing Recursive Methods -

the Process

  • The process for writing recursive robot instructions is very similar to that for writing loops:
  • Step 1 : Consider the stopping condition (also called the base case)--what is the simplest case of the problem that can be solved? In the pickAll problem, the simplest, or base, case is when the robot is already on an empty corner.

the Process - cont’d

  • Step 4 : Make sure the reduction leads to the base case. Again, in the above example of pickAll, by picking up one beeper at a time, the robot must eventually clear the corner of beepers, regardless of the original number present.

Iteration vs. Recursion

  • An iterative loop must complete each iteration before beginning the next one.
  • A recursive method typically begins a new instance before completing the current one. When that happens, the current instance is temporarily suspended, pending the completion of the new instance. Of course, this new instance might not complete before generating another one. Each successive instance must be completed in turn, last to first.

Follow the steps and

write the method

  • Use recursion to move a robot named Karel to a beeper. Follow the steps presented earlier:
  • Step 1: What is the base case?
    • Karel is on the beeper.
  • Step 2: What does the robot do in the base case?
    • Nothing.
  • Step 3: What is the general case?
    • The robot is not on the beeper.

writing the recursive method - cont’d

  • Step 4: What is the reduction (what do we do in the general case)? - Move toward the beeper and make the recursive call.
  • Does the reduction lead to termination? Yes, assuming the beeper is directly in front of the robot, the distance will get shorter by one block for each recursive call. By the way, this “assumption” becomes the precondition(i.e., there is a beeper somewhere in front of the bot).

let’s write it

  • Step 1: What is the base case?
    • Karel is on the beeper
  • Step 2: What does Karel have to do in the base case? - turnLeft (this will face Karel north)
  • Step 3: What is the general case?
    • Karel is not on the beeper

let’s write it

  • Step 4: What is the reduction?
    • Move one block forward, make the recursive call and have Karel execute a second move after the recursive call. This second move will be executed in all instances but the base case, causing Karel to make as many moves north after the base case as it did in getting to the base case.
  • Now, code it before we go on…