Tahir Naseem/Handout 5
-1-
Theory of Automata and Formal Languages
Lecture 5
Objectives
What is Recursive Function?
Recursive definition of languages
What is Recursive Function?
Recursive function is a function that calls itself until a certain condition is
met. That condition is called the base case. This enables the function to repeat
itself several times, outputting the result and the end of each iteration.
Example of a recursive function.
function Count (integer N)
if (N <= 0) return "Must be a Positive Integer";
if (N > 9) return "Counting Completed";
else return Count (N+1);
end function
The function Count () above uses recursion to count from any
number between 1 and 9, to the number 10. For example, Count (1)
would return 2,3,4,5,6,7,8,9,10. Count (7) would return 8,9,10. The result
could be used as a roundabout way to subtract the number from 10.
Recursive functions are common in computer science because they
allow programmers to write efficient programs using a minimal amount of
code. The downside is that they can cause infinite loops and other
unexpected results if not written properly. For example, in the example
above, the function is terminated if the number is 0 or less or greater than
9. If proper cases are not included in the function to stop the execution,
the recursion will repeat forever, causing the program to crash, or worse
yet, hang the entire computer system.
Recursive definition of languages: The following three steps are used in recursive definition
1. Some basic words are specified in the language.
2. Rules for constructing more words are defined in the language.
3. No strings except those constructed in above, are allowed to be in the
language.
Tahir Naseem/Handout 5
-2-
Theory of Automata and Formal Languages
Examples
1. Defining language of INTEGER
Step 1: 1 is in INTEGER.
Step 2: If x is in INTEGER then x+1 and x-1 are also in INTEGER.
Step 3: No strings except those constructed in above, are allowed to be in
INTEGER.
Language INTEGER = {,-2,-1,0,1,2,}
Explanation: To give a recursive definition of the set of integers, think
about how to generate integers from the simplest integer, namely 1.
Integers are either positive or negative. The positive integers can be
generated by adding 1 to 1 one or more times to get 1, 2,3,4, The
negative integers can be generated by subtracting 1 from 1 to get -1 , -2 ,
-3 etc.
2. Defining language of EVEN
Step 1: 0 is in EVEN.
Step 2: If x is in EVEN then x+2 and x-2 are also in EVEN.
Step 3: No strings except those constructed in above, are allowed to be in
EVEN.
Language EVEN = {,-4,-2, 0, 2, 4,}
Explanation: To give a recursive definition of the set of even integers,
think about how to generate even integers from the simplest integer,
namely 0.
Even integers are either positive or negative. The positive even integers
can be generated by
adding 2 to 0 one or more times to get 2,4,6,8, The negative even
integers can be generated by
subtracting 2 from 0.
Tahir Naseem/Handout 5
-3-
Theory of Automata and Formal Languages
3. Defining the language L, of strings containing aa or bb ,
defined over ={a, b}
Step 1: aa and bb are in L
Step 2: s(aa)s and s(bb)s are also in L, where s belongs to *
Step 3: No strings except those constructed in above, are allowed to be in
L
Language L = { , aa , bb , aaaa, baab , abba , bbbb , }
4. Defining the language factorial
Step 1: As 0!=1, so 1 is in factorial.
Step 2: n!=n*(n-1)! is in factorial.
Step 3: No strings except those constructed in above, are allowed to be in
factorial.
Language Factorial = {1,2,6,24,120,}
5. Defining the language L, of strings beginning and ending
in same letters, defined over ={a, b}
Step 1: a and b are in L
Step 2: (a)s(a) and (b)s(b) are also in L, where s belongs to *
Step 3: No strings except those constructed in above, are allowed to be in
L
Language L= { , aa , bb , aaa , bbb , aba , bab , }
6. Defining the language PALINDROME, defined over =
{a,b}
Step 1: a and b are in PALINDROME
Step 2: if x is palindrome, then s(x)Rev(s) and xx will also be palindrome,
where s belongs to *
Step 3: No strings except those constructed in above, are allowed to be in
palindrome
Language PALINDROME={ , a, b, aa, bb, aaa, aba, bab, bbb, ...}
Tahir Naseem/Handout 5
-4-
Theory of Automata and Formal Languages
Recursive definition of L*:
Step 1:
L*
Step 2: For any x
L* and any w
L, xw
L*.
Step 3: Nothing is in L* unless it is obtained from the above two clauses.
L* is the set of strings obtained by concatenating zero or more strings of.
This * is called Kleene star. For example if L = { aba, bb }, then L* = {
aba, bb, ababb, abaaba, bbbb, bbaba, ... }
Recursive definition of L+
Step 1 : L
L+
Step 2 : For any x
L+ and any w
L, xw
L+ .
+
Step 3 : Nothing is in L unless it is obtained from the above two
clauses.
Thus L+ is the set of strings obtained by concatenating one or more
strings of L.
For example if L = { aba, bb }, then L+ = { aba, bb, ababb, abaaba, bbbb,
bbaba, ... }
Do problems 1 to 9, 14, 18 and 19.