next up previous
Contents Next: Exercises Up: Programming Techniques Previous: Abstraction

Summary of Rules

1.
When recurring on a list, do three things:
a.
check for the termination condition;
b.
use the first element of the list;
c.
recur with the ``rest'' of the list.

2.
If a function builds a list using ``cons,'' return () at the terminating line.
3.
When recurring on a nested list, do three things:
a.
check for the termination condition;
b.
check if the first element of the list is an atom or a list;
c.
recur with the ``first'' and the ``rest'' of the list.

4.
When evaluating an expression, do three things:
a.
check for the termination condition;
b.
identify operator;
c.
apply operator to recursive calls on the operands.

5.
When building a number value using +, return 0 at the terminating line. When building a number value using *, return 1 at the terminating line.
6.
When recurring on a number, do three things:
a.
check for the termination condition;
b.
use the number in some form;
c.
recur with a changed form of the number.

7.
To ensure proper termination do two things:
a.
make sure that you are changing at least one argument in your recursive call;
b.
make sure that your test for termination looks at the arguments that change in the recursive call.

8.
Two simple cases may occur when changing an argument in a recursive call:
a.
if you are using ``rest'' to change an argument which is a list, use ``null'' in the test for termination;
b.
if you are decreasing an argument which is a number, compare it with 0 in the test for termination.

9.
Use ``let'' to reduce the number of function calls.

10.
Encapsulate program fragments into new functions to improve clarity.

11.
Encapsulate repeated program fragments into new functions to reduce program size.



next up previous
Contents Next: Exercises Up: Programming Techniques Previous: Abstraction



© Colin Allen & Maneesh Dhagat
March 2007