HW16: Recursion Lab

Due Wed 5/28.

Preamble

The goal of this lab is to investigate recursive algorithms and think through how they work.

You may work with a partner for this lab.

The Lab

Download recursionExamples.py and open it in your text editor. It contains definitions for a few recursive functions. Read through them and try to understand how they work. Add some code that calls the functions (with small arguments), and maybe some debug output. Take notes on paper, tracing through the execution. Think through what's going on.

It's tricky to keep track of where control flow is going when you're running recursive functions. To help you visualize this, I've prepared these examples on pythontutor.com, which lets you step through short programs and watch where the execution point goes and how values in memory change. Click on any of the links below, change the code as you see fit, and click Visualize Execution. Then go through the execution step by step, and think about why the program is behaving the way it is.

There is no hand-in for this assignment, and it doesn't count for any points. However, for our next class I will assume you've done this assignment. It's also good preparation for the next homework assignment, which has you writing some recursive functions of your own.

Start early and ask lots of questions!