# Class 33: Numeric Recursion

This outline is also available in PDF.

Held: Monday, April 5, 2010

Summary: We visit a slightly different kind of recursion, numeric recursion. In this technique, we once again have procedures call themselves. However, the parameter that we simplify at every step is a number, rather than a list.

Related Pages:

Notes:

• Welcome back! I hope you had a great break.
• Reading for tomorrow: Geometric Art.
• Exams returned. We'll spend a good deal of class going over them. Hence, the numeric recursion lab is an assignment due on Wedesnday.
• EC for today's Disability Studies talk on making a movie (4:15 in JRC101).
• EC for tonight's Disability Studies movie (7:00 in ARH 302).
• EC for tomorrow's CS and Disability talk (4:30 in Science 3821).
• EC for Wednesday's CS and Disability talk (4:15 in JRC101).
• EC for Thursday's "Teaching Millenials" CS Extra (4:30 in Science 3821).
• EC for Friday's "Computational Games" CS Extra (noon in Science 3821; Free Pizza; I'll need a count on Wednesday).

Overview:

• Recursion, Generalized.
• Thinking About Natural Numbers.
• Numeric Recursion.

## Patterns of Recursion

• While we've seen and written a variety of examples of direct recursion, they typically have the following form:
```(define recursive-proc
(lambda (params)
(if (base-case-test)
(base-case params)
(combine (partof params)
(recursive-proc (simplify params))))))
```
• In many cases, the combination ends up being a choice between two activities. In those cases, we might write:
```(define recursive-proc
(lambda (params)
(cond
((base-case-test)
(base-case params))
((special-case-test)
(combine (partof params)
(recursive-proc (simplify params))))
(else
(recursive-proc (simplify params))))))
```
• For lists, the simplification was almost always take the cdr and the part-of was almost always take the car.

## Recursion with Numbers

• While most of the recursion we've been doing has used lists as the structure to recurse over, you can recurse with many different kinds of values.
• It is fairly common to recurse using numbers.
• The natural base cases for integers are when you hit 0 or when you hit 1.
• The natural simplification step for recursive procedure using numbers calls typically involves subtracting 1 from the argument.
• Other simplifications, such as dividing in half, are also possible.

## Lab

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 11 09:03:13 2010.
The source to the document was last modified on Thu Jan 21 13:05:15 2010.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2010S/Outlines/outline.33.html`.

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.) This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.