Functional Problem Solving (CSC 151 2016S) : Outlines

Outline 28: Recursion with Helper Procedures


Held: Friday, 11 March 2016

Back to Outline 27 - Preconditions, Revisited. 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

Reminders

Upcoming Work:

Extra Credit

Academic / Artistic

Peer

Regular Peer

Misc

Far in the Future

No Extra Credit, But Still Good

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                     ()