 \chapter{Recursion and exceptions}

\section{Recursive data structures}
All of the Python data types we have seen can be grouped inside lists
and tuples in a variety of ways. Lists and tuples can also be nested,
providing myriad possibilities for organizing data. The organization
of data for the purpose of making it easier to use is called a
\myidx{data structure}.

It's election time and we are helping to compute the votes as they
come in. Votes arriving from individual wards, precincts,
municipalities, counties, and states are sometimes reported as a sum
total of votes and sometimes as a list of subtotals of votes. After
considering how best to store the tallies, we decide to use a
\emph{nested number list}, which we define as follows:

A \emph{nested number list} is a list whose elements are either:
\begin{enumerate}
\item numbers
\item nested number lists
\end{enumerate}

Notice that the term, nested number list is used in its own
definition. \myidx{Recursive definitions} like this are quite common
in mathematics and computer science. They provide a concise and
powerful way to describe \myidx{recursive data structures} that are
partially composed of smaller and simpler instances of
themselves. The definition is not circular, since at some point we
will reach a list that does not have any lists as elements.
Now suppose our job is to write a function that will sum all of the
values in a nested number list. Python has a built-in function which
finds the sum of a sequence of numbers:
\begin{pythonInteractive}
>>> sum([1, 2, 8])
11
>>> sum((3, 5, 8.5))
16.5
>>>
\end{pythonInteractive}
For our \emph{nested number list}, however, \pythoninline{sum} will
not work: 
\begin{pythonInteractive}
>>> sum([1, 2, [11, 13], 8])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> 
\end{pythonInteractive}
The problem is that the third element of this list, \pythoninline{[11,
13]}, is itself a list, which can not be added to \pythoninline{1},
\pythoninline{2}, and \pythoninline{8}. 

\section{Recursion}
To sum all the numbers in our recursive nested number list we need to
traverse the list, visiting each of the elements within its nested
structure, adding any numeric elements to our sum, and
\emph{repeating this process} with any elements which are lists.
Modern programming languages generally support \myidx{recursion} ,
which means that functions can \emph{call themselves} within their
definitions. Thanks to recursion, the Python code needed to sum the
values of a nested number list is surprisingly short:
\begin{python}
def recursive_sum(nested_num_list):
    sum = 0
    for element in nested_num_list:
        if type(element) == type([]):
            sum = sum + recursive_sum(element)
        else:
            sum = sum + element
    return sum
\end{python}
The body of \pythoninline{recursive\_sum} consists mainly of a
\pythoninline{for} loop that traverses
\pythoninline{nested\_num\_list}. If \pythoninline{element} is a
numerical value (the else branch), it is simply added to
\pythoninline{sum}. If \pythoninline{element} is a list, then
\pythoninline{recursive\_sum} is called again, with the element as an
argument. The statement inside the function definition in which the
function calls itself is known as the \myidx{recursive call} .

Recursion is truly one of the most beautiful and elegant tools in
computer science.

A slightly more complicated problem is finding the largest value in
our nested number list:
\begin{python}
def recursive_max(nested_num_list):
    """
      >>> recursive_max([2, 9, [1, 13], 8, 6])
      13
      >>> recursive_max([2, [[100, 7], 90], [1, 13], 8, 6])
      100
      >>> recursive_max([2, [[13, 7], 90], [1, 100], 8, 6])
      100
      >>> recursive_max([[[13, 7], 90], 2, [1, 100], 8, 6])
      100
    """
    largest = nested_num_list[0]
    while type(largest) == type([]):
        largest = largest[0]

    for element in nested_num_list:
        if type(element) == type([]):
            max_of_elem = recursive_max(element)
            if largest < max_of_elem:
                largest = max_of_elem
        else:                           # element is not a list
            if largest < element:
                largest = element

    return largest
\end{python}
Doctests are included to provide examples of
\pythoninline{recursive\_max} at work.
The added twist to this problem is finding a numerical value for
initializing largest. We can't just use
\pythoninline{nested\_num\_list[0]}, since
that my be either a number or a list. To solve this problem we use a
while loop that assigns largest to the first numerical value no
matter how deeply it is nested.

The two examples above each have a \myidx{base case} which does not
lead to a recursive call: the case where the element is a number and
not a list. Without a base case, you have \myidx{infinite recursion},
and your program will not work. Python stops after reaching a 
maximum recursion depth and returns a runtime error.

Write the following in a file named
\pythoninline{infinite\_recursion.py}:
\begin{python}
#
# infinite_recursion.py
#
def recursion_depth(number):
    print "Recursion depth number %d." % number
    recursion_depth(number + 1)

recursion_depth(0)
\end{python}
At the unix command prompt in the same directory in which you saved
your program, type the following:
\begin{shell}
$ python infinite_recursion.py
\end{shell}
After watching the messages flash by, you will be presented with the
end of a long traceback that ends in with the following:
\begin{pythonOutput}
  ...
  File "infinite_recursion.py", line 3, in recursion_depth
    recursion_depth(number + 1)
RuntimeError: maximum recursion depth exceeded
\end{pythonOutput}
We would certainly never want something like this to happen to a user
of one of our programs, so before finishing the recursion discussion,
let's see how errors like this are handled in Python.

\section{Exceptions}
Whenever a runtime error occurs, it creates an \myidx{exception}. The
program stops running at this point and Python prints out the 
traceback, which ends with the exception that occured.
For example, dividing by zero creates an exception:
\begin{pythonInteractive}
>>> print 55/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>
\end{pythonInteractive}
So does accessing a nonexistent list item:
\begin{pythonInteractive}
>>> a = []
>>> print a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>
\end{pythonInteractive}
Or trying to make an item assignment on a tuple:
\begin{pythonInteractive}
>>> tup = ('a', 'b', 'd', 'd')
>>> tup[2] = 'c' 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
\end{pythonInteractive}
In each case, the error message on the last line has two parts: the
type of error before the colon, and specifics about the error after
the colon.
Sometimes we want to execute an operation that might cause an
exception, but we don't want the program to stop. We can
\myidx{handle the exception} using the \pythoninline{try} and
\pythoninline{except} statements. 
For example, we might prompt the user for the name of a file and then
try to open it. If the file doesn't exist, we don't want the program
to crash; we want to handle the exception:
\begin{python}
filename = raw_input('Enter a file name: ')
try:
    f = open (filename, "r")
except:
    print 'There is no file named', filename
\end{python}
The \pythoninline{try} statement executes the statements in the first
block. If no 
exceptions occur, it ignores the except statement. If any exception
occurs, it executes the statements in the except branch and then
continues.
We can encapsulate this capability in a function: \pythoninline{exists} takes a
filename and returns true if the file exists, false if it doesn't:
\begin{python}
def exists(filename):
    try:
        f = open(filename)
        f.close()
        return True 
    except:
        return False
\end{python}
You can use multiple \pythoninline{except} blocks to handle different
kinds of exceptions (see the Errors and Exceptions lesson from Python
creator Guido van Rossum's Python Tutorial for a more complete
discussion of exceptions). 
If your program detects an error condition, you can make it
\myidx{raise} an exception. Here is an example that gets input from
the user and checks that the number is non-negative. 
\textbf{NOTE: this is a bad example of using exceptions, the function
should gracefully handle bad user input, not raise an exception.}
\begin{python}
#
# learn_exceptions.py
#
def get_age():
    age = input('Please enter your age: ')
    if age < 0:
        raise ValueError, '%s is not a valid age' % age
    return age
\end{python}
The \pythoninline{raise} statement takes two arguments: the exception
type, and specific information about the error.
\pythoninline{ValueError} is the built-in exception which most closely
matches the kind of error we want to raise. The complete listing of
built-in exceptions is found in section 2.3 of the Python Library
Reference, again by Python's creator, Guido van Rossum. 


If the function that called \pythoninline{get\_age} handles the error, then the
program can continue; otherwise, Python prints the traceback and
exits:
\begin{pythonInteractive}
>>> get_age()
Please enter your age: 42
42 
>>> get_age()
Please enter your age: -2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "learn_exceptions.py", line 4, in get_age
    raise ValueError, '%s is not a valid age' % age
ValueError: -2 is not a valid age
>>>
\end{pythonInteractive}
The error message includes the exception type and the additional
information you provided.
Using exception handling, we can now modify
\pythoninline{infinite\_recursion.py} so
that it stops when it reaches the maximum recursion depth allowed:
\begin{python}
#
# infinite_recursion.py
#
def recursion_depth(number):
    print "Recursion depth number %d." % number
    try:
        recursion_depth(number + 1)
    except:
        print "Maximum recursion depth exceeded."

recursion_depth(0)
\end{python}
Run this version and observe the results.

\section{Tail recursion}

When a recursive call occurs as the last line of a function
definition, it is refered to as \myidx{tail recursion}.
Here is a version of the \pythoninline{countdown} function from Section
\ref{sec-while-statement} written using tail recursion:
\begin{python}
def countdown(n):
    if n == 0:
        print "Blastoff!"
    else:
        print n
        countdown(n-1)
\end{python}
Any computation that can be made using iteration can also be made
using recursion.

Several well known mathamatical functions are defined
recursively. Factorial, for example, is given the special operator,
\pythoninline{!}, and is defined by:
\begin{eqnarray*}
0! &:=& 1\\
n! &:=& n(n-1)
\end{eqnarray*}
We can easily code this into Python:
\begin{python}
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
\end{python}
Another well know recursive relation in mathematics is the fibonacci
sequence, which is defined by:
\begin{eqnarray*}
fibonacci(0) &:=& 1\\
fibonacci(1) &:=& 1\\
fibonacci(n) &:=& fibonacci(n-1) + fibonacci(n-2)
\end{eqnarray*}
This can also be written easily in Python:
\begin{python}
def fibonacci (n):
    if n == 0 or n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)
\end{python}
Both \pythoninline{factorial} and \pythoninline{fibonacci} are
examples of tail recursion. 

Tail recursion is considered a bad practice in languages like Python,
however, since it uses more system resources than the equivalent
iterative solution.
Calling \pythoninline{factorial(1000)} will exceed the maximum
recursion depth. And try running \pythoninline{fibonacci(35)} and see
how long it takes to complete (be patient, it will complete). 
You will be asked to write an iterative version of
\pythoninline{factorial} as an 
exercise, and we will see a better way to handle
\pythoninline{fibonacci} in the 
next chapter.

\section{List comprehensions}
A \myidx{list comprehension} is a syntactic construct that enables
lists to be created from other lists using a compact, mathematical
syntax:
\begin{pythonInteractive}
>>> numbers = [1, 2, 3, 4]
>>> [x**2 for x in numbers]
[1, 4, 9, 16]
>>> [x**2 for x in numbers if x**2 > 8]
[9, 16] 
>>> [(x, x**2, x**3) for x in numbers]
[(1, 1, 1), (2, 4, 8), (3, 9, 27), (4, 16, 64)]
>>> files = ['bin', 'Data', 'Desktop', '.bashrc', '.ssh', '.vimrc']
>>> [name for name in files if name[0] != '.']
['bin', 'Data', 'Desktop']
>>> letters = ['a', 'b', 'c']
>>> [n*letter for n in numbers for letter in letters]
['a', 'b', 'c', 'aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc', 'aaaa', 'bbbb', 'cccc']
>>>
\end{pythonInteractive}
The general syntax for a list comprehension expression is:
\begin{python}
[expr for item1 in seq1 for item2 in seq2 ... for itemx in seqx if condition]
\end{python}
This list expression has the same effect as:
\begin{python}
output_sequence = []
for item1 in seq1:
    for item2 in seq2:
        ...
            for itemx in seqx:
                if condition:
                    output_sequence.append(expr)
\end{python}
As you can see, the list comprehension is much more compact.

\section{Mini case study: tree}
The following program implements a subset of the behaviour of the Unix
tree program.
\begin{python}
#!/usr/bin/env python

import os
import sys

def getroot():
    if len(sys.argv) == 1:
        path = ''
    else:
        path = sys.argv[1]

    if os.path.isabs(path):
        tree_root = path
    else:
        tree_root = os.path.join(os.getcwd(), path)

    return tree_root

def getdirlist(path):
    dirlist = os.listdir(path)
    dirlist = [name for name in dirlist if name[0] != '.']
    dirlist.sort()
    return dirlist

def traverse(path, prefix='|--', s='.\n', f=0, d=0):
    dirlist = getdirlist(path)

    for num, file in enumerate(dirlist):
        lastprefix = prefix[:-3] + '`--'
        dirsize = len(dirlist)

        if num < dirsize - 1:
            s += '%s %s\n' % (prefix, file)
        else:
            s += '%s %s\n' % (lastprefix, file)
        path2file = os.path.join(path, file)

        if os.path.isdir(path2file):
            d += 1
            if getdirlist(path2file):
                s, f, d = traverse(path2file, '|   ' + prefix, s, f, d)
        else:
            f += 1

    return s, f, d


if __name__ == '__main__':
    root =  getroot()
    tree_str, files, dirs = traverse(root)

    if dirs == 1:
        dirstring = 'directory'
    else:
        dirstring = 'directories'
    if files == 1:
        filestring = 'file'
    else:
        filestring = 'files'

    print tree_str
    print '%d %s, %d %s' % (dirs, dirstring, files, filestring)
\end{python}
You will be asked to explore this program in several of the exercises
below.

\section{Glossary}
 \begin{description}
 \item[immutable data type:]A data type which cannot be
   modified. Assignments to elements or slices of immutable types
   cause a runtime error.
 \item[mutable data type:]A data type which can be modified. All
   mutable types are compound types. Lists and dictionaries (see next
   chapter) are mutable data types; strings and tuples are not.
 \item[tuple:]A data type that contains a sequence of elements of any
   type, like a list, but is immutable. Tuples can be used wherever an
   immutable type is required, such as a key in a dictionary (see next
   chapter).
 \item[tuple assignment:]An assignment to all of the elements in a
   tuple using a single assignment statement. Tuple assignment occurs
   in parallel rather than in sequence, making it useful for swapping
   values.
 \item[data structure]An organization of data for the purpose of
   making it easier to use.
 \item[recursive definition:]A definition which defines something in
   terms of itself. To be useful it must include \emph{base cases}
   which are not recursive. In this way it differs from a
   \emph{circular definition} . Recursive definitions often provide an
   elegant way to express complex data structures.
 \item[recursion:]The process of calling the function that is
   currently executing.
 \item[recursive call:]The statement in a recursive function with is a
   call to itself.
 \item[base case:]A branch of the conditional statement in a recursive
   function that does not result in a recursive call.
 \item[infinite recursion:]A function that calls itself recursively
   without ever reaching the base case. Eventually, an infinite
   recursion causes a runtime error.
 \item[exception:]An error that occurs at runtime.
 \item[handle an exception:]To prevent an exception from terminating a
   program using the try and except statements.
 \item[raise:]To signal an exception using the raise statement.
 \item[tail recursion:]A recursive call that occurs as the last
   statement (at the tail) of a function definition. Tail recursion is
   considered bad practice in Python programs since a logically
   equivalent function can be written using \emph{iteration} which is
   more efficient (see the Wikipedia article on tail recursion for
   more information).
 \item[list comprehension:]A syntactic construct which enables lists
   to be generated from other lists using a syntax analogous to the
   mathematical set-builder notation.
\end{description}

\section{Laboratory Exercises}
\begin{enumerate}
 \item
\begin{python}
def swap(x, y):      # incorrect version
     print  "before swap statement: id(x):", id(x), "id(y):", id(y)
     x, y = y, x
     print  "after swap statement: id(x):", id(x), "id(y):", id(y)

a, b = 0, 1
print  "before swap function call: id(a):", id(a), "id(b):", id(b)
swap(a, b)
print  "after swap function call: id(a):", id(a), "id(b):", id(b)
\end{python}
Run this program and describe the results. Use the results to explain
why this version of \pythoninline{swap} does not work as intended.
What will be the values of \pythoninline{a} and \pythoninline{b} after
the call to \pythoninline{swap}? 

\item Create a module named \pythoninline{seqtools.py}. Add the
functions \pythoninline{encapsulate} and
\pythoninline{insert\_in\_middle} from the chapter. Add doctests which
test that these two functions work as intended with all three sequence
types. 

\item Add each of the following functions to
\pythoninline{seqtools.py}:
\begin{python}
def make_empty(seq):
    """
      >>> make_empty([1, 2, 3, 4])
      []
      >>> make_empty(('a', 'b', 'c'))
      ()
      >>> make_empty("No, not me!")
      ''
    """

def insert_at_end(val, seq):
    """
      >>> insert_at_end(5, [1, 3, 4, 6])
      [1, 3, 4, 6, 5]
      >>> insert_at_end('x', 'abc')
      'abcx'
      >>> insert_at_end(5, (1, 3, 4, 6))
      (1, 3, 4, 6, 5)
    """

def insert_in_front(val, seq):
    """
      >>> insert_in_front(5, [1, 3, 4, 6])
      [5, 1, 3, 4, 6]
      >>> insert_in_front(5, (1, 3, 4, 6))
      (5, 1, 3, 4, 6)
      >>> insert_in_front('x', 'abc')
      'xabc'
    """

def index_of(val, seq, start=0):
    """
      >>> index_of(9, [1, 7, 11, 9, 10])
      3
      >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5))
      3
      >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
      6
      >>> index_of('y', 'happy birthday')
      4
      >>> index_of('banana', ['apple', 'banana', 'cherry', 'date'])
      1
      >>> index_of(5, [2, 3, 4])
      -1
      >>> index_of('b', ['apple', 'banana', 'cherry', 'date'])
      -1
    """

def remove_at(index, seq):
    """
      >>> remove_at(3, [1, 7, 11, 9, 10])
      [1, 7, 11, 10]
      >>> remove_at(5, (1, 4, 6, 7, 0, 9, 3, 5))
      (1, 4, 6, 7, 0, 3, 5)
      >>> remove_at(2, "Yomrktown")
      'Yorktown'
    """

def remove_val(val, seq):
    """
      >>> remove_val(11, [1, 7, 11, 9, 10])
      [1, 7, 9, 10]
      >>> remove_val(15, (1, 15, 11, 4, 9))
      (1, 11, 4, 9)
      >>> remove_val('what', ('who', 'what', 'when', 'where', 'why', 'how'))
      ('who', 'when', 'where', 'why', 'how')
    """

def remove_all(val, seq):
    """
      >>> remove_all(11, [1, 7, 11, 9, 11, 10, 2, 11])
      [1, 7, 9, 10, 2]
      >>> remove_all('i', 'Mississippi')
      'Msssspp'
    """

def count(val, seq):
    """
      >>> count(5, (1, 5, 3, 7, 5, 8, 5))
      3
      >>> count('s', 'Mississippi')
      4
      >>> count((1, 2), [1, 5, (1, 2), 7, (1, 2), 8, 5])
      2
    """

def reverse(seq):
    """
      >>> reverse([1, 2, 3, 4, 5])
      [5, 4, 3, 2, 1]
      >>> reverse(('shoe', 'my', 'buckle', 2, 1))
      (1, 2, 'buckle', 'my', 'shoe')
      >>> reverse('Python')
      'nohtyP'
    """

def sort_sequence(seq):
    """
      >>> sort_sequence([3, 4, 6, 7, 8, 2])
      [2, 3, 4, 6, 7, 8]
      >>> sort_sequence((3, 4, 6, 7, 8, 2))
      (2, 3, 4, 6, 7, 8)
      >>> sort_sequence("nothappy")
      'ahnoppty'
    """

if __name__ == "__main__":
    import doctest
    doctest.testmod()
\end{python}
As usual, work on each of these one at a time until they pass all
of the doctests.

\item Write a function, \pythoninline{recursive\_min}, that returns
the smallest value in a nested number list: 
\begin{python}
def recursive_min(nested_num_list):
    """
      >>> recursive_min([2, 9, [1, 13], 8, 6])
      1
      >>> recursive_min([2, [[100, 1], 90], [10, 13], 8, 6])
      1
      >>> recursive_min([2, [[13, -7], 90], [1, 100], 8, 6])
      -7
      >>> recursive_min([[[-13, 7], 90], 2, [1, 100], 8, 6])
      -13
    """
\end{python}
Your function should pass the doctests.

\item Write a function \pythoninline{recursive\_count} that returns
the number of occurences of \pythoninline{target} in
\pythoninline{nested\_number\_list}:
\begin{python}
def recursive_count(target, nested_num_list):
    """
      >>> recursive_count(2, [2, 9, [2, 1, 13, 2], 8, [2, 6]])
      4
      >>> recursive_count(7, [[9, [7, 1, 13, 2], 8], [7, 6]])
      2
      >>> recursive_count(15, [[9, [7, 1, 13, 2], 8], [2, 6]])
      0
      >>> recursive_count(5, [[5, [5, [1, 5], 5], 5], [5, 6]])
      6
    """
\end{python}
As usual, your function should pass the doctests.

\item Write a function flatten that returns a simple list of numbers
containing all the values in a \pythoninline{nested\_number\_list}:
\begin{python}
def flatten(nested_num_list):
    """
      >>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
      [2, 9, 2, 1, 13, 2, 8, 2, 6]
      >>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
      [9, 7, 1, 13, 2, 8, 7, 6]
      >>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
      [9, 7, 1, 13, 2, 8, 2, 6]
      >>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
      [5, 5, 1, 5, 5, 5, 5, 6]
    """
\end{python}
Run your function to confirm that the doctests pass.

\item Write a function named \pythoninline{readposint} that prompts
the user for a positive integer and then checks the input to confirm
that it meets the requirements. A sample session might look like this:
\begin{pythonInteractive}
>>> num = readposint()
Please enter a positive integer: yes
yes is not a positive integer.  Try again.
Please enter a positive integer: 3.14
3.14 is not a positive integer.  Try again.
Please enter a positive integer: -6
-6 is not a positive integer.  Try again.
Please enter a positive integer: 42
>>> num
42
>>> num2 = readposint("Now enter another one: ")
Now enter another one: 31
>>> num2
31
>>>
\end{pythonInteractive}
Use Python's exception handling mechanisms in confirming that the
user's input is valid.

\item Give the Python interpreter's response to each of the
following: 
\begin{enumerate}
\item
\begin{pythonInteractive}
>>> nums = [1, 2, 3, 4]
>>> [x**3 for x in nums]
\end{pythonInteractive}
\item
\begin{pythonInteractive}
>>> nums = [1, 2, 3, 4]
>>> [x**2 for x in nums if x**2 != 4]
\end{pythonInteractive}
\item
\begin{pythonInteractive}
>>> nums = [1, 2, 3, 4]
>>> [(x, y) for x in nums for y in nums]
\end{pythonInteractive}
\item
\begin{pythonInteractive}
>>> nums = [1, 2, 3, 4]
>>> [(x, y) for x in nums for y in nums if x != y]
\end{pythonInteractive}
\end{enumerate}
You should anticipate the results \emph{before} you try them in the
interpreter.

\item Use either pydoc or the on-line documentation at
\url{http://pydoc.org} to find out what
\pythoninline{sys.getrecursionlimit()} and 
\pythoninline{sys.setrecursionlimit(n)} do. Create several
\emph{experiments} like what was done in
\pythoninline{infinite\_recursion.py} to test your understanding
of how these module functions work.

\item Rewrite the factorial function using iteration instead of
recursion. Call your new function with 1000 as an argument and make
note of how fast it returns a value.

\item  Write a program named \pythoninline{litter.py} that creates an
empty file named \pythoninline{trash.txt} in each subdirectory of a
directory tree given the root of the tree as an argument (or the
current directory as a default). Now write a program named
\pythoninline{cleanup.py} that removes all these files. \\
\\
\emph{Hint:} Use the tree program from the mini case study as a
basis for these two recursive programs.
\end{enumerate}


