Functional Problem Solving (CSC 151 2015S) : Outlines

Outline 28: Recursion with Helper Procedures


Held: Monday, 9 March 2015

Back to Outline 27 - Characters and Strings. On to Outline 29 - Recursion with Helper Procedures, Continued.

Summary

We consider a different form of recursion, one based on the construction of recursive helpers that take additional parameters. Along the way, we consider the idea of tail recursion. We also explore how careless design of recursive procedures can inadvertently lead to slow execution.

Related Pages

Overview

Administrivia

Upcoming Work

Extra Credit Opportunities

Academic

Peer Support (Morning Section)

Peer Support (Afternoon Section)

Miscellaneous (Extra Credit)

Other Good Things (No Extra Credit)

Assorted Exam Notes and Questions

I know I can't ask you a direct question about the exam, but could you answer the following?

You can ask me a direct question about the exam. I may not answer it, or a I may answer it obliquely. But you are certainly permitted to ask.

Any hints?

I've added hints to a few problems in which people asked for hints, particularly the one on speeding up code. You will also find it useful to look more deeply at the Q and A section of the exam, because the questions people ask can provide hints.

Have two people spent at least four hours on the exam or completed the exam?

Yes. That's among the reasons that there are now only seven problems.

Q (from Sam): What do you expect to get from the expression (> x (and y z))? (I think this is intended to mean "if x is greater than both y and z".) I find it useful to think about concrete values: What is (> 3 (and 1 5))?

A (from "volunteer"):

Basics of Helper Recursion

Tail Recursion

Clarity in Tail Recursion

Some programmers find tail recursion much easier to understand. It's certainly easier to chart. And that may make it easier to design. We can also keep track of what's going on a bit better.

Let's do an example: Let's count the number of bright colors in a list.

What should we keep track of along the way?

When are we done?

What do we return?

What do we do in other situations?

Note: For whatever reason, I see some students who find tail recursion very natural and standard recursion confusing, and some students who find standard recursion confusing and tail recursion natural. I'll push you to work on both.

Old Notes

These are from a previous time I taught the class. I leave them around for historical reasons.

Delayed Evalution in Recursive Procedures

Helper Recursion

  partial-sum            unexplored-elements
  0                      (2 3 5 7 11 13)
  2                      (3 5 7 11 13)
  5                      (5 7 11 13)
  8                      (7 11 13)
  15                     (11 13)
  26                     (13)
  39                     ()