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

Section 3.2: Recursively Defined Functions and Procedures: A Recursive Function (Or Procedure) Calls Itself!

The document describes recursive functions and procedures. It provides examples of recursively defined functions, including functions that sum lists of numbers, concatenate strings, check if an element is in a list, check if one list is a substring of another, check if an element is in a binary search tree, and traverse binary trees in preorder, inorder, and postorder. Recursive definitions are provided for each function in terms of smaller inputs, with base cases also given. Programming-like formulations using if-then-else statements are also provided.

Uploaded by

Prerna Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Section 3.2: Recursively Defined Functions and Procedures: A Recursive Function (Or Procedure) Calls Itself!

The document describes recursive functions and procedures. It provides examples of recursively defined functions, including functions that sum lists of numbers, concatenate strings, check if an element is in a list, check if one list is a substring of another, check if an element is in a binary search tree, and traverse binary trees in preorder, inorder, and postorder. Recursive definitions are provided for each function in terms of smaller inputs, with base cases also given. Programming-like formulations using if-then-else statements are also provided.

Uploaded by

Prerna Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Section 3.

2: Recursively Defined Functions


and Procedures
Function: Has inputs (arguments, operands) and output (result)
No side effects.
Procedure: May have side effects, e.g., print()
A recursive function (or procedure) calls itself!
A function f is recursively defined if at least one value of f(x) is defined
in terms of another value, f(y), where xy.

Similarly: a procedure P is recursively defined if the action of P(x) is


defined in terms of another action, P(y), where xy.

When an argument to a function is inductively defined, here is a technique


for creating a recursive function definition:
1. Specify a value of f(x) for each basis element x in S.
2. For each inductive rule that defines an element x in S in terms of
some element y already in S, specify rules in the function
that compute f(x) in terms of f(y).

CS340-Discrete Structures Section 3.2 Page 1


Example: Find a recursive definition for function f:NN defined by
f(n) = 0 + 3 + 6 + + 3n.
e.g., f(0) = 0
f(1) = 0 + 3
f(2) = 0 + 3 + 6
Solution: Notice that N is an inductively defined set:
0N; nN implies n+1N
So we need to give f(0) a value and
we need to deinfe f(n+1) in terms of f(n).
The value for f(0) should be 0. What about f(n+1)?
f(n+1) = 0 + 3 + 6 + 3n + 3(n+1)
= f(n) + 3(n+1)

So here is our (recursive) definition for f:


f(0) = 0
f(n+1) = f(n)+3(n+1)
We could also write:
f(0) = 0
f(n) = f(n-1)+3n for n>0
Here is a more programming-like definition:
f(n) = ( if n=0 then 0 else f(n-1)+3n endIf )

CS340-Discrete Structures Section 3.2 Page 2


Example: Find a recursive definition for
cat: A* A* A*
defined by cat(s,t) = st

Solution: Notice that A* is inductively defined.


Basis: A*; Induction: aA and xA* imply axA*

We can define cat recursively using the first argument.

The definition of cat gives


cat(,t) = t = t.
For the recursive part we can write
cat(ax,t) = axt = a(xt) = acat(x,t)

Here is a definition:
cat(,t) = t
cat(ax,t) = acat(x,t)

Here is the if-then-else form:


cat(s,t) = if s= then t else head(s)cat(tail(s),t)

CS340-Discrete Structures Section 3.2 Page 3


Example: Find a definition of f:lists(Q)Q defined by
f(<x1, , xn>) = x1 + + xn

Solution: Notice that the set lists(Q) is defined rescursively.


Basis: <>lists(Q)
Induction: hQ and tlists(Q) imply h::tlists(Q)

To discover a recursive definition, we can use the definition of f as follows:


f(<x1, , xn>)
= x1 + x2 + xn
= x1 + (x2 + + xn)
= x1 + f(<x2, ,xn>)
= head(<x1, , xn>) + f(tail(<x1, , xn>))

So, here is our recursive definition:


f(<>) = 0
f(h::t) = h + f(t)
Expressing this in the if-then-else form:
f(L) = if L=<> then 0 else head(L)+f(tail(L))

CS340-Discrete Structures Section 3.2 Page 4


Example: Given f:NN as defined by
f(0) = 0
f(1) = 0
f(x+2) = 1+f(x)

Here is the if-then-else formulation:


f(x) = if (x=0 or x=1) then 0 else 1 + f(x-2)

What exactly does this function do?

Lets try to get an idea by enumerating a few values.


map(f,<0,1,2,3,4,5,6,7,8,9>) = <0,0,1,1,2,2,3,3,4,4>

So f(x) returns the floor of x/2. That is, f(x) = x/2.

CS340-Discrete Structures Section 3.2 Page 5


Example: Find a recursive definition for the function f:lists(Q)Q
as defined by:
f(<x1, , xn>) = x1x2 + x2x3 + + xn-1xn

Approach:
Let f(<>) = 0 and f(<x>) = 0. Then for n2 we can write:
f(<x1, , xn>)
= x1x2 + x2x3 + + xn-1xn
= x1x2 + (x2x3 + + xn-1xn)
= x1x2 + f(<x2, , xn>)
So here is our recursive definition:
f(<>) = 0
f(<x>) = 0
f(h::t) = hhead(t) + f(t).

We can express this in if-then-else form as:


f(L) = if (L=<> or tail(L)=<>)
then
0
else
head(L)head(tail(L)) + f(tail(L))
endIf
CS340-Discrete Structures Section 3.2 Page 6
Example: Find a recursive definition for the function
isin : A lists(A) {true,false}
where isin(x,L) means that x occurs in the list L.

Solution:
isin(x,<>) = false
isin(x,x::t) = true
isin(x,y::t) = isin(x,t), where x y

Heres the if-then-else form:

isin(x,L) = if L=<>
then
false
else
if x=head(L)
then
true
else
isin(x,tail(L))
endIf
endIf
CS340-Discrete Structures Section 3.2 Page 7
Example: Find a recursive definition for the function
isin : A lists(A) {true,false}
where isin(x,L) means that x occurs in the list L.

Solution:
isin(x,<>) = false
isin(x,x::t) = true
isin(x,y::t) = isin(x,t), where x y

Heres the if-then-else form:

isin(x,L) = if L=<>
then
false
else
x=head(L) or isin(x,tail(L))
endIf

CS340-Discrete Structures Section 3.2 Page 8


Example: Find a recursive definition for
sub: lists(A) lists(A) {true,false}
where sub(L,M) means the elements of L are elements of M.
Solution: From Previous Slide
Here is a pattern-matching solution:
sub(<>,M) = true
sub(h::t,M) = if isin(h,M) then sub(t,M) else false
Here is a programmatic (executable) version:
sub(L,M) = if L=<>
then
true
else
if isin(head(L),M)
then
sub(tail(L),M)
else
false
endIf
endIf

CS340-Discrete Structures Section 3.2 Page 9


Example: Find a recursive definition for
intree: Q binSearchTrees(Q) {true,false}
where intree(x,T) means x is in the binary search tree T.
Solution:
intree(x,<>) = false
intree(x,<L,x,R>) = true
intree(x,<L,y,R>) = if x<y then intree(x,L) else intree(x,R)

Why is this a better definition?


intree(x,<>) = false
true, if x=y
intree(x,<L,y,R>) = intree(x,L), if x<y
intree(x,R), if x>y

Here is the if-then-else form:


intree(x,T) = if T=<> then false
elseIf x=root(T) then true
elseIf x<root(T) then intree(x,left(T))
else intree(x,right(T))
endIf

CS340-Discrete Structures Section 3.2 Page 10


Traversing Binary Trees

There are 3 ways to traverse a binary tree.


Each is defined recursively.

preorder(T): if T<> then


visit root; preorder(left(T)); preorder(right(T))
inorder(T): if T<> then
inorder(left(T)); visit root; inorder(right(T))
postorder(T): if T<> then
postorder(left(T)); postorder(right(T); visit root

Example: Traverse this tree in each of the orders:


a

b c Solution:
pre-order:
in-order:
d e
post-order:

CS340-Discrete Structures Section 3.2 Page 11


Traversing Binary Trees

There are 3 ways to traverse a binary tree.


Each is defined recursively.

preorder(T): if T<> then


visit root; preorder(left(T)); preorder(right(T))
inorder(T): if T<> then
inorder(left(T)); visit root; inorder(right(T))
postorder(T): if T<> then
postorder(left(T)); postorder(right(T); visit root
The parentheses are
Example: Traverse this tree in each of the orders:
not part of the
a answer, but adding
them makes things
clearer.
b c Solution:
pre-order: a (b d e) (c)
in-order: (d b e) a (c)
d e
post-order: (d e b) (c) a

CS340-Discrete Structures Section 3.2 Page 12


Example: Find a recursive definition for
post: binaryTrees(A) lists(A)
where post(T) is the list of nodes from a pot-order traversal of T.

Solution:
post(<>) = <>
post(<L,x,R>) = cat(post(L),cat(post(R),<x>)

The function cat will concatenate two lists, and can be defined as:
cat(<>,L) = L
cat(h::t,L) = h::cat(t,L)

Example: Find a recursive definition for


sumnodes: binaryTrees(Q) Q
where sumnodes(T) returns the sum of the nodes in T.

Solution:
sumnodes(<>) = 0
sumnodes(<L,x,R>) = x + sumnodes(L) + sumnodes(R)

CS340-Discrete Structures Section 3.2 Page 13


Infinite Sequences
We can construct recursive definitions for infinite sequences by defining a
value f(x) in terms of x and f(y) for some value y in the sequence.

Example: Suppose we want to define a function f that returns an infinite


sequence. The function f should return this sequence:
f(x) = <x1, x2, x4, x8, x16, >
Approach:
Look at the definition and try to find a solution:
f(x)
= <x1, x2, x4, x8, x16, >
= x :: <x2, x4, x8, x16, >
= x :: f(x2)
So we can define:
f(x) = x :: f(x2)
This function returns an infinite sequence.

Q: Of what use is such a function in computing???


A: We can use lazy evaluation: When we need an element from f(x),
well need to evaluate f. Yes, this is an infinite computation, but well
do only as much work as necessary to get the element we need.

CS340-Discrete Structures Section 3.2 Page 14


Example: What sequence is defined by g(x,k)= xk :: g(x,k+1)?

Solution: g(x,k) = xk :: g(x,k+1)


= xk :: xk+1 :: g(x,k+2)
= <xk, xk+1, xk+2, >

Example: How do we obtain the sequence <x, x3, x5, x7, >?

Solution: Define f(x) = h(x,1)


where h(x,k) = xk :: h(x,k+2)

Example: How do we obtain the sequence <1, x2, x4, x6, x8, >?

Solution: Define f(x) = h(x,0), where h is from the previous example.

CS340-Discrete Structures Section 3.2 Page 15

You might also like