Home

About

Advanced Search

Browse by Discipline

Scientific Societies

E-print Alerts

Add E-prints

E-print Network
FAQHELPSITE MAPCONTACT US


  Advanced Search  

 
The Allan Technique of Programming Recursively Students have difficulty with recursion. I once had a professor who said that recursion would become
 

Summary: The Allan Technique of Programming Recursively
Students have difficulty with recursion. I once had a professor who said that recursion would become
as natural for me as iteration. Although I doubted him at the time, it has become true. Here are a few
steps I have picked up over the years.
1. Decide that the problem needs a recursive solution. Likely candidates are operations on a
recursive structure (such as trees) or problems which are defined recursively (like Ackermann's
function).
Some people avoid recursion as it is more costly than iteration. I believe that good compilers
will eventually eliminate most of the overhead of recursive calls. Plus, the convenience and
maintainability of recursive routines make them a good choice.
2. Decide specifically what the procedure will do. What are its inputs and what are its outputs. In
a sentence, what does it accomplish?
3. Restate the problem recursively. Look for instances of the problem you defined in step 2.
If the procedure returns a value, recursive calls to the function must use the returned values and
every path through the function must return a value.
4. Write the procedure using instances of itself. Only worry about a single call of the routine. This
is similar to proof by induction. You only solve for one case, but all other cases take care of
themselves.
Do not be tempted to follow the recursion. Use the routines having the faith that they will work.
(It is a self-esteem thing. Believe in yourself!) If you try to follow the recursion while you are

  

Source: Allan, Vicki H. - Department of Computer Science, Utah State University

 

Collections: Computer Technologies and Information Sciences