The Y Combinator

In this file we derive the Y combinator, one of the fundamental results
of recursive procedure theory. You already know that in some cases it
is not necessary to give a procedure a name. For example,

((lambda (x) (+ x 1)) 6)

adds 1 to 6 without naming the procedure that does it. But, what about
a recursive procedure? For example,

(define fact
(lambda (n)
(if (zero? n)
1
(* n (fact (- n 1))))))

which computes the factorial of a number n, seems to need the name "fact"
so that in the last line of the procedure it can recurse on itself. But,
we will see this is not necessary and, in the process, will develop a lot
of intuition about using Scheme. We proceed step by step, changing "fact"
slightly on each step.

Step 1. The first idea is simply to pass "fact" in as an argument
in much the same way that we did for

(define op-maker
(lambda (op)
(lambda (x y)
(op x y))))

The first lambda passes the name of the operation and the second
lambda is the nameless operation. Let's try this with "fact".
The first attempt is

The idea will be to pass "fact-maker" in through "procedure". Thus,
what we would like to do is invoke (fact-maker fact-maker) to produce our
nameless (well, almost nameless) factorial procedure. This would
allow us to write, for example

>((fact-maker fact-maker) 5)
120

But, this doesn't work because "fact-maker" is a procedure which takes
as input one argument that is a procedure but "procedure",
which is supposed to be identical to "fact", requires a numeric argument.
The solution is the following:

Well, we got the name out of the body of the procedure but we
still have to pass the procedure in and so far we have been using
a name to do that. So let's try to get
the whole dependence on a name out.

Step 2. Recall we demand that "fact" be identical to
(procedure procedure) which in turn must be identical to
(fact-maker fact-maker) (recall the example ((fact-maker fact-maker) 5)
which gives the same result as (fact 5)). Thus, we can write "fact-maker"
in the following way, making use of the result of step 1.

This produces the factorial of 5 because the procedure which
is invoked (the huge mess) is exactly the definition of "fact."
But, lo and behold, there is no name for this procedure anywhere!

In what follows, we try to generalize this to all procedures and
wind up with the dreaded applicative-order Y-combinator.

Step 3. First, we need to separate out the part that pertains to
computing the factorial. The goal is to write this part in one
place and when code for other problems is substituted for the
factorial code, the result will be a new recursive procedure.
This step is a little tricky because we insist on using, with no
significant changes, code that was designed assuming a procedure name.
The section of factorial code we currently have,
from step 2, is

This is different from what we want because it contains a
(procedure procedure) where we would like to see a plain old
procedure. So, we use a trick to get it out. In general, isn't

(f arg)

identical to

((lambda (x) (f x)) arg) ?

The second statement is a little strange, though, because it
makes you pass "arg" into a procedure so that the procedure which
would be applied to it anyway is applied. Why do we want to
do such a thing? Watch! This means that

You will probably want to study this carefully. Notice the
double left parens in front of ((lambda (func-arg)... This is
because we are writing

...
((lambda (func-arg) < body-using-func-arg >) (lambda (arg) ...))

which has the same form as

((lambda (arg) ((procedure procedure) arg)) (- n 1))

but is different in that a procedure is passed as an "arg" instead
of an integer.

The two expressions beginning with (lambda (func-arg) ...)
are exactly the pieces of code that correspond to the factorial
code and they are in exactly the right form. So we can get them
out of the definition of fact in the following way:

This is significant because we can now use any procedure in place
of F* to change functionality to whatever we want. The only
problem is that, as written, we still need to name F*. This is
easily remedied in the next step.

Step 5. Jackpot! Now we write the dreaded applicative-order
Y-combinator:

As an assignment for the interested student, write findamx
without using the procedure name "max". Just how many of the
remaining names in findmax do you think can be disposed of?
Talk about a nameless society...