In computer science, anonymous recursion is a recursion technique that uses anonymous functions.
Construction
Suppose that f is an n-argument recursive function defined in terms of itself:

We want to define an equivalent recursive anonymous function
which is not defined in terms of itself. One possible procedure is the following:
- Define a (n + 1)-argument function h like f, except that h takes one extra argument, which is the function f itself. (Function h inherits its first n arguments from f.)
- Change f, the last argument of h, from an n-argument function to an (n + 1)-argument function by feeding f to itself as f′s last argument for all instances of f within the definition of h. (This puts function h and its last argument f on an equal footing.) The functions f and h are now defined by


- Pass on h to itself as h's own last argument, and let this be the definition of the desired n-argument recursive anonymous function
:

Example
Given

The function f is defined in terms of itself: such circular definitions are not allowed in anonymous recursion (because functions are not bound to labels). To start with a solution, define a function h(x,f) in exactly the same way that f(x) was defined above:

Second step: change
in the definition to
:

so that the function f passed on to h will have the same number of arguments as h itself.
Third step: the factorial function
of x can now be defined as:

Relation to the use of the Y combinator
The above approach to constructing anonymous recursion differs, but is related to, the use of fixed point combinators. To see how they are related, perform a variation of the above steps. Starting from the recursive definition (using the language of lambda calculus):

First step,

Second step,

where
. Note that the variation consists of defining
in terms of
instead of in terms of
.
Third step: let a "Z combinator" be defined by

such that

Here,
is passed to itself right before a number is fed to it, so
for any Church number n.
Note that
, i.e.
.
Now an extra fourth step: notice that

(see first and second steps) so that


Let the Y combinator be defined by

so that

External links