Recursion Guidelines:

Example of recursion without a list:

add from 1 to a max value:


(define (addnum max)
(cond
[ ( = max 0) 0 ]
[ else ( + max (addnum (- max 1))) ]
))


(addnum 5) ; gives 15

Recursion Thought Process to reach this code:

1) What is true about the problem? (truth statements will end up in your code)
2) What are the base cases? (small simple truths - adding up 0 numbers yields 0)
3) What are you taking in and what is being returned ? ( give a max and get a total)
4) Make some samples:
Addnum(0) should give 0
Addnum(1) should give 1
Addnum(2) should give 3
Addnum(3) should give 6
Addnum(4) should give 10


Coding the recursion:

1) Define the function without a body giving names to input arguments

(define (addnum num ) )

2) Fill in the body with a cond

(cond [ ( ) ]
[ else ])


3) Put the base case into the first condition

(cond [ ( num <= 0 ) 0 ]
[ else ])

4) Coding the Recursive Call

a) Consider how to handle one part of the repetition;

think about one of the later calls as a sample (addnum 4)

b) Write what is available to you

Your input arguments
Good return values from your function (see your tests)

c) Figure out how to get the rest of the information when the one part in step a is removed

d) Call that part recursively


(cond [ (<= num 0 ) 0 ]
[ else num + addnum(num-1) ])