Docsity

Recursive Programming: Understanding Recursion and Recursive Definitions in CS112, Slides of C programming

Toggle sidebar
bg1
1
CS 112 Introduction to
Programming
Lecture #28:
Recursive Programming
http://flint.cs.yale.edu/cs112/
2
Recursive Programming
rRecursion: a method calls itself
rRecursion is often used
to search for a solution
to a problem, e.g.,
mHanoi Towers
mMaze search
mChange maker: how
many ways of making changes
to a one-dollar bill?
rRecursion is also useful for modeling
physical world
mFractal images
1 2 3
3
Recursive Thinking
rA recursive definition is one which uses the
word or concept being defined in the
definition itself
rWhen defining an English word, a recursive
definition is often not helpful, e.g.,
mA is not B; B is not A
rBut in other situations, a recursive definition
can be an appropriate way to express a
concept
rBefore applying recursion to programming, it
is best to practice thinking recursively
4
Recursive Definition: LIST
rConsider the following list of numbers:
24, 88, 40, 37
rSuch a list can be defined as
A LIST is a: number
or a: number comma LIST
rThat is, a LIST is defined to be a single number, or a number
followed by a comma followed by a LIST
rThe concept of a LIST is used to define itself
5
Recursive Definitions:
Matching LIST Definition
number comma LIST
24 , 88, 40, 37
number comma LIST
88 , 40, 37
number comma LIST
40 , 37
number
37
rThe recursive part of the LIST definition is
used several times, terminating with the non-
recursive part.
6
Recursive Definitions: Factorial
rN!, for any positive integer N, is defined to be
the product of all integers between 1 and N
inclusive
rThis definition can be expressed recursively
as:
1! = 1
N! = N * (N-1)!
rThe concept of the factorial is defined in
terms of another factorial
pf3

Partial preview of the text

Download Recursive Programming: Understanding Recursion and Recursive Definitions in CS112 and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #28:

Recursive Programming

http://flint.cs.yale.edu/cs112/

2

Recursive Programming

r Recursion: a method calls itself

r Recursion is often used

to search for a solution

to a problem, e.g.,

m Hanoi Towers

m Maze search

m Change maker: how

many ways of making changes

to a one-dollar bill?

r Recursion is also useful for modeling

physical world

m Fractal images

3

Recursive Thinking

r A recursive definition is one which uses the

word or concept being defined in the

definition itself

r When defining an English word, a recursive

definition is often not helpful, e.g.,

m A is not B; B is not A

r But in other situations, a recursive definition

can be an appropriate way to express a

concept

r Before applying recursion to programming, it

is best to practice thinking recursively

4

Recursive Definition: LIST

r Consider the following list of numbers:

r Such a list can be defined as

A LIST is a: number or a: number comma LIST

r That is, a LIST is defined to be a single number, or a number

followed by a comma followed by a LIST

r The concept of a LIST is used to define itself

5

Recursive Definitions:

Matching LIST Definition

number comma LIST

number comma LIST

number comma LIST

number

r The recursive part of the LIST definition is

used several times, terminating with the non-

recursive part.

6

Recursive Definitions: Factorial

r N!, for any positive integer N, is defined to be

the product of all integers between 1 and N

inclusive

r This definition can be expressed recursively

as:

N! = N * (N-1)!

r The concept of the factorial is defined in

terms of another factorial

7

Recursive Definitions: Factorial

8

Traditional Method Calls

r Flow of control flows from one method to

another and returns as specified

Foo Bar

Foo(); Bar();

Main

9

Recursive Method Calls

r What if a method calls itself?

void LotsOfPrints( )

Console.WriteLine( “inside LotsOfPrints” );

LotsOfPrints();

LotsOfPrints

LotsOfPrints

LotsOfPrints

LotsOfPrints

LotsOfPrints

LotsOfPrints

10

Infinite Recursion

r All recursive definitions have

to have a non-recursive part,

e.g., use some condition

m The non-recursive part is often called the base case

r If they didn't, there would be

no way to terminate the

recursive path

r Such a definition would

cause infinite recursion

void LotsOfPrints() { if ( some-condition ) { do non-recursive computation; } else { Console.WriteLine( “in LotsOfPrints” ); LotsOfPrints(); } }

11

Termination (contd.)

r Introduce an input variable

r Value of input variable determines

termination

void LotsOfPrints( int num )

if ( num == 0 )

Console.WriteLine( “the bug stops here!” );

} else

Console.WriteLine( “inside LotsOfPrints” );

LotsOfPrints( num – 1 );

12

Control Flow

LotsOfPrints(2)

LotsOfPrints(1)

LotsOfPrints(1)

LotsOfPrints(0)

LotsOfPrints(0)