Session 15
Recursive Programming Review

a 2x2 grid using the meme Drakeposting meme.  The upper left is Drake looking away, shielded by his hand. In the upper right is the phrase 'NO INFINITE RECURSION'. In the lower left, Drake smiles and points ahead. The lower right panel is an image of the full 2x2 comic, recursive in the lower right panel.
Yes, Dr. Wallingford is Drakeposting about recursion.
Source: Reddit

Where We Are

For the last few weeks, we have been learning techniques for writing recursive programs based on inductively-defined data. These include one base technique and three ideas to use when the basic technique needs a little help:

We then began using structural recursion to write recursive functions to answer questions about programs in a little language consisting only of variable references, one-argument functions, and applications (function calls). (occurs-bound? var exp) was the first of several functions that process expressions in the little language.

At the same time, we began using syntax procedures defined for the little language, which allowed us to focus on the meaning of our data rather than their implementation in Racket. Syntax procedures give our little language what we have come to expect from Racket's built-in data types: accessors, type predicates, and constructors.

Today, let's review many of these ideas as you begin to prepare for the quiz. We'll go over problems from Homework 6 and do a few practice problems from a previous Quiz 2.

Review of Homework 6

Problem 1, tails
"Think carefully about what the function should return if it receives the empty list as an argument. This is also the base case for the recursion."
Problem 2, n-list?
This function is a type predicate for n-lists. To implement similar functions for flat lists, we could use map and apply. (In this case, we would use andmap because we can't apply and booleans). Mutual recursion makes this function almost as easy to implement over the nested lists.
Problem 3, tree-min
Trees cannot be empty: A tree is either a number or a list of size three, containing a number and two trees. This function does not need a null? case, and it does not need to recur on the cdr of a list.
Problem 4, declared-vars
To solve this problem, you first need to understand that only lambda expressions declare variables. With that knowledge, the BNF description guides us toward a solution.
Problem 5, prefix->postfix
The solution is straightforward. Only apps call functions, so our function needs to change only app expressions — and any expressions that contain apps. Follow the data structure... and use the syntax procs.

Two general notes:

Practice Problems

Many students have been asking for more ways to practice writing recursive functions, or to see examples of Quiz 2 problems. Today, let's try to satisfy both kinds of request by working these practice problems drawn from previous Quiz 2s:

These functions all refer to data types you have worked with multiple times before. If you follow the structure of the data closely, each is relatively short. The problems on your quiz will have both of these features, too.

Note: we skipped (annotate prefix-exp) in class. The solution is the code file.

Other Ideas to Review

What other kinds of questions might appear on the quiz? We have studied a number of ideas and techniques that you will want to understand:

You will also want to be able to discuss the ideas and techniques linked in Where We Are above, and why we use them.

Wrap Up