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

Section 3.4 Recursive Definitions

The document discusses recursive definitions of functions, sequences, sets, strings, identifiers, arithmetic expressions, and subclasses of strings. Recursive definitions specify a basis rule that gives the initial value and a recursion rule that defines each subsequent value in terms of the previous ones. Examples are given of recursively defining factorials, mergesort, the Hanoi sequence, the Fibonacci sequence, and other concepts using basis and recursion rules.

Uploaded by

theresa.painter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Section 3.4 Recursive Definitions

The document discusses recursive definitions of functions, sequences, sets, strings, identifiers, arithmetic expressions, and subclasses of strings. Recursive definitions specify a basis rule that gives the initial value and a recursion rule that defines each subsequent value in terms of the previous ones. Examples are given of recursively defining factorials, mergesort, the Hanoi sequence, the Fibonacci sequence, and other concepts using basis and recursion rules.

Uploaded by

theresa.painter
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Section 3.

Recursive Denitions

3.4.1

3.4 RECURSIVE DEFINITIONS


Functions can be dened recursively. The simplest form of recursive denition of a function f on the natural numbers species abasis rule (B) the value f (0) and a recursion rule (R) how to obtain f (n) from f (n 1), n 1 Example 3.4.1: (B ) (R ) n-factorial n!

0! = 1 (n + 1)! = (n + 1) n!

However, recursive denitions often take somewhat more general forms. Example 3.4.2: mergesort (A[1 . . . 2n ]: real) if n = 0 return(A) otherwise return(merge (msort(1st half), msort(2nd half)))
Coursenotes by Prof. Jonathan L. Gross for use with Rosen: Discrete Math and Its Applic., 5th Ed.

Chapter 3

MATHEMATICAL REASONING

3.4.2

Since a sequence is dened to be a special kind of a function, some sequences can be specied recursively. Example 3.4.3: Hanoi sequence 0, 1, 3, 7, 15, 31, . . . h0 = 0 hn = 2hn1 + 1 for n 1 Example 3.4.4: Fibonacci seq 1, 1, 2, 3, 5, 8, 13, . . . f0 = 1 f1 = 1 fn = fn1 + fn2 for n 2 Example 3.4.5:
n

aj =
j =0

partial sums of sequences a0 if n = 0 n1 otherwise j =0 aj + an

Example 3.4.6: Catalan sequence 1, 1, 2, 5, 14, 42, . . . c0 = 1 cn = c0 cn1 + c1 cn2 + + cn1 c0 for n 1

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

Section 3.4

Recursive Denitions

3.4.3

RECURSIVE DEFINITION of SETS def: A recursive denition of a set S comprises the following: (B) a basis clause that species a set of primitiveelements; (R) a recursive clause that species how elements of the set may beconstructed from elements already known to be in set S ; there may be several recursive subclauses; (E) an implicit exclusion clause that anything not in the set as aresult of the basis clause or the recursive clause is not in set S . Backus Normal Form (BNF) is an example of a context-free grammar that is usefulfor giving resursive denitions of sets. In W3261, you will learn that context-free languages are recognizable by pushdown automata.

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

Section 3.4

Recursive Denitions

3.4.5

RECURSIVE DEFINITION of STRINGS notation: The set of all strings in the alphabet is generally denoted . Example 3.4.8: binary strings. {0, 1} denotes the set of all

def: string in an alphabet (B) (empty string) is a string; (R) If s is a string and b , then sb is a string. Railroad Normal Form for strings

Example 3.4.9:

BNF for strings

string ::= | string character

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

Chapter 3

MATHEMATICAL REASONING

3.4.6

RECURSIVE DEFINITION of IDENTIFIERS def: An identier is (for some programming languages) either (B) a letter, or (R) an identier followed by a digit or a letter.

Example 3.4.10:

BNF for identiers

lowercase letter ::= a | b | | z uppercase letter ::= A | B | | Z letter ::= lowercase letter | uppercase letter digit ::= 0 | 1 | | 9 identier ::= letter | identier letter | identier digit

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

Section 3.4

Recursive Denitions

3.4.7

ARITHMETIC EXPRESSIONS def: arithmetic expressions (B) A numeral is an arithmetic expression. (R) If e1 and e2 are arithmetic expressions, then all of the following are arithmetic expressions: e1 + e2 , e1 e2 , e1 e2 , e1 /e2 , e1 e2 , (e1 ) Example 3.4.11: Backus Normal Form

expression ::= numeral | expression + expression | expression expression | expression expression | expression / expression | expression expression | ( expression )

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

Chapter 3

MATHEMATICAL REASONING

3.4.8

SUBCLASSES of STRINGS Example 3.4.12: binary strings of even length (B) S (R) If b S , then b00, b01, b10, b11 S . Example 3.4.13: binary strings of even length that start with 1 (B) 10, 11 S (R) If b S , then b00, b01, b10, b11 S . def: A strict palindrome is a character string that is identical to itsreverse. (In natural language, blanks and other punctuation are ignored, as is thedistinction between upper and lower case letters.) Able was I ere I saw Elba. Madam, Im Adam. Eve. Example 3.4.14: set of binary palindromes (B) , 0, 1 S (R) If x S then 0x0, 1x1 S .

Coursenotes by Prof.

Jonathan L. Gross for use with Rosen:

Discrete Math and Its Applic., 5th Ed.

You might also like