next up previous
Next: About this document Up: My Home Page

Defining Functions

Functions with a finite domain can be described by specifying for each element in the domain the associated element in the codomain.

The two basic mechanisms for defining functions on infinite domains, such as the natural numbers tex2html_wrap_inline381 , are

An explicit definition of a function f consists of giving an expression that indicates for each domain element x how f(x) is obtained from previously defined functions (including constants) by composition.

Examples.

displaymath345

The last function is called the characteristic function of the set A.

If-then-else may be used for case distinctions in function definitions.

Recursive Definitions

A recursive definition of a function consists of giving an expression for every domain element x that indicates how f(x) is obtained from previously defined functions and values of f for ``smaller'' arguments (by composition).

Such definitions are also called recursive. They are based on a recursion principle, which specifies under which conditions such definitions with self-references are well-formed.

For example, the number of permutations of n elements is n! (or fact(n), read n factorial). This function can be defined recursively by:

displaymath346

The values fact(n), for all n>0, depend on values fact(k), where k is smaller than n.

displaymath347

Example: Squares

The same function can usually be defined in different ways.

For instance, the function that squares its argument can be defined explicitly in terms of multiplication,

displaymath348

or by recursion:

displaymath349

From the recursive definition we get the following function values:

displaymath350

The two definitions above define the same function, as

displaymath351

or, equivalently,

displaymath352

Fibonacci Numbers

The recursive definition of the following well-known function employs the function values for several smaller arguments:

displaymath353

The corresponding function values are called Fibonacci numbers:

displaymath354

The Fibonacci numbers were originally defined to count the number of rabbits after n generations, but they pop up in an amazing variety of places:

Partially Defined Functions

A key requirement of a recursive definition is that it be formulated in terms of function values for smaller arguments.

Consider this definition,

displaymath355

and corresponding attempts at computing function values,

displaymath356

This function is defined for one argument only!

What about the function G, defined for positive integers by

displaymath357

Is it well-defined for all arguments?

We have

displaymath358

Thus, if G(5) were defined, we could infer the contradictory statement that 0=3! In other words, G(5) must be undefined.

It has been conjectured (and shown up to one trillion) that a slight modification,

displaymath359

defines a function on all positive integers.

2-1

10-5-16-8-4-2-1

17-52-26-13-40-20-10-5-16-8-4-2-1

21-64-32-16-8-4-2-1

35-106-53-160-80-40-20-10-5-16-8-4-2-1

Note that if this function is well-defined, then the value of H(n) counts the number of downward steps this path takes, i.e H(2) = 1 and H(35) = 10.

Well-Formed Recursive Definitions

A well-formed recursive definition of a function f consists of two parts:

In the case of definitions of functions over the natural numbers, smaller is interpreted in the usual sense.

Later on we will see recursive definitions of functions on other domains, such as lists, where ``smaller'' necessarily has to be interpreted differently.

Computing Values of Recursively Defined Functions

The evaluation of a recursively defined function for a specific argument involves two kinds of operations:

Example.

displaymath360

The evaluation process will terminate if the definition is well-formed.

More General Recursive Definitions

Example.

displaymath361

This function is known as ``McCarthy's 91 function.'' Its definition uses nested recursive function applications.

Is this function defined for all arguments tex2html_wrap_inline499 ?

Consider one instance,

displaymath362

The function is in fact defined for all positive integers and remarkably takes the value 91 for all arguments less than or equal to 101.

Recursion - Summary

Recursion is a general method for the definition of functions (and also a powerful technique for designing algorithms).

Recursive definitions generally specify only partial functions. (Recall that a partial function satisfies the uniqueness, but not the completeness property required of total functions.)

If a recursive definition employs only values of the defined function for smaller arguments, it defines a total function.

The evaluation of recursively defined function for specific arguments is based on calculation by substitution and simplification.

These two concepts,

form the foundation of functional programming languages such as ML.




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Aug 24 20:37:27 EDT 1999