The Ten Commandments
-
When recurring on a list of atoms, , ask two questions about it:
(null? lat)
andelse
. When recurring on a number, , ask two questions about it:(zero? n)
andelse
. When recurring on a list of S-expressions, , ask three questions about it:(null? l)
,(atom? (car l))
, andelse
. -
Use
cons
to build lists. -
When building a list, describe the first typical element, and then
cons
it onto the natural recursion. -
Always change at least one argument while recurring. When recurring on a list of atoms, , use
(cdr lat)
. When recurring on a number, , use(sub1 n)
. And when recurring on a list of S-expressions, , use(car l)
and(cdr l)
if neither(null? l)
nor(atom? (car l))
are true. It must be changed to be closer to termination. The changing argument must be tested in the termination condition:- when using
cdr
, test termination withnull?
. - when using
sub1
, test termination withzero?
.
- when using
-
When building a value with
+
, always use0
for the value of terminating line, for add0
does not change the value of an addition. When building a value with*
, always use1
for the value of terminating line, for multiplying by1
does not change the value of multiplication. When building a value withcons
, always consider()
for the value of the value of the terminating line. -
Simplify only after the function is correct.
-
Recur on the
subparts
that are of the same nature:- On the sublists of a list.
- On the subexpressions of an arithmetic expression.
-
Use help function to abstract from representations.
-
Abstract common patterns with a new function.
-
Build functions to collect more than one value at a time.