0% found this document useful (0 votes)
40 views

03 Recursion

The document discusses recursion and provides rules for writing recursive functions. It defines recursion as a definition that uses the thing being defined. There are base cases, which are simple problems that can be solved directly without recursion, and recursive cases that break the problem into a simpler subproblem and extra work. The four rules for writing recursive functions are to first do the base cases, only recur with a simpler case, avoid using global variables or passing by reference, and focus on the current level without considering other levels.

Uploaded by

mukesh427
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

03 Recursion

The document discusses recursion and provides rules for writing recursive functions. It defines recursion as a definition that uses the thing being defined. There are base cases, which are simple problems that can be solved directly without recursion, and recursive cases that break the problem into a simpler subproblem and extra work. The four rules for writing recursive functions are to first do the base cases, only recur with a simpler case, avoid using global variables or passing by reference, and focus on the current level without considering other levels.

Uploaded by

mukesh427
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Recursion

Definitions I
A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list consists of:
An open parenthesis, "(" Zero or more atoms or lists, and A close parenthesis, ")"

Definitions II
Indirect recursion is when a thing is defined in terms of other things, but those other things are defined in terms of the first thing Example: A list is:
An open parenthesis, Zero or more S-expressions, and A close parenthesis

An S-expression is an atom or a list

Understanding recursion
The usual way to teach recursion is to trace through a recursion, seeing what it does at each level This may be a good way to understand how recursion works... ...but it's a terrible way to try to use recursion There is a better way

Base cases and recursive cases


Every valid recursive definition consists of two parts:
One or more base cases, where you compute the answer directly, without recursion One or more recursive cases, where you do part of the work, and recur with a simpler problem

The four rules


Do the base cases first Recur only with a simpler case Don't use global or reference variables Don't look down

Do the base cases first


Every recursive function must have some things it can do without recursion These are the simple, or base, cases Test for these cases, and do them first This is just writing ordinary, nonrecursive code

Recur only with a simpler case


If the problem isn't simple enough to be a base case, break it into two parts:
A simpler problem of the same kind (for example, a smaller number, or a shorter list) Extra work not solved by the simpler problem

Combine the results of the recursion and the extra work into a complete solution Simpler means more like a base case

It's OK to use locals variables and parameters passed by value


A function has its own copy of
local variables parameters passed by value

Each level of a recursive function has its own copy of these variables and parameters Changing them at one level does not change them at other levels One level can't interfere with another level
9

It's bad to use global variables or parameters passed by reference


There is only one copy of a global variable If a parameter is passed by reference, there is only one copy of it If such a variable is changed by a recursive function, it's changed at all levels The various levels interfere with one another This can get very confusing Don't let this happen to you!
10

Don't look down


When you write or debug a recursive function, think about this level only Wherever there is a recursive call, assume that it works correctly If you can get this level correct, you will automatically get all levels correct You really can't understand more than one level at a time, so don't even try

11

The End

12

You might also like