In this post, we discuss the Y Combinator (no, not the company!) and attempt to gently introduce you to the derivation of the construct through small bite-sized refactoring steps done on a simple recursive function. The example is in Racket (and can run in Scheme if you replace the λ with lambda). It is simple enough to be ported without effort into most languages, functional-first or not.
Contents
- Why the Y Combinator?
- A simple path to the Y Combinator
- An infinite loop
- Making the infinite loop stop
- Making the infinite loop do something useful
- Interlude: Why does it recurse?
- Delayed Evaluations vs. Infinity
- Interlude: Why does this not recurse infinitely?
- Expansion: The Attack of the Parentheses
- An Alternative Path: Refactoring the Total Function
- Conclusion
- References
I have been revisiting “The Little Schemer” recently, and I keep continually getting reminded what a gem of a language Lisp is (any variant). Though I have arrived at Functional Programming through various angles, it was never a straight line; instead being a series of progressive dead ends, half-completed exercises, re-reading things from the start, applying some concepts over and over again in languages not originally meant to be functional, going over-zealous about it and burning myself…oh so many detours.
I still consider myself mostly a novice in this area. So, I started re-reading the part about the Y Combinator, followed through the “length” exercises, and then promptly lost the plot after a little while. What followed after that was watching a bunch of videos, reading multiple well-intentioned articles and Github gists, using Jane Street’s Socratic Tutor skill - it’s not that great, btw - and of course, the mandatory shouting at Claude Code in order to cover up my own frustrations of not being able to build up a mental model that I was comfortable with.
This post is an attempt to distill all of those hours of banging my head to come up with a coherent model of how the Y combinator works. Of course, none of this is new: it has been around since Haskell Curry discovered it, and there are tons of good explainers of the idea out there. This is just my spin on it.
I will be using Racket / Scheme for this post. I actually ran all of it on Racket, and the original Scheme interpreter works just as well, as long as you replace the λs with lambdas. I will not deny that I find walls of parentheses rather unreadable beyond a certain threshold, but that probably speaks to how code should be structured in the first place, rather than how readable a Lisp is. In any case, as we will see, the constraints of the problem that leads us to the Y Combinator, somewhat force us to deal with more parentheses than I would normally find comfortable.
Why the Y Combinator?
Most material about the Y-Combinator treats its rationale from a couple of different angles.
- It proves that you do not need unbound names to do recursion.
- It lets you build recursion in languages which do not support recursion, i.e., do not allow reference to functions by names.
- It lets you separate out the actual step computation from control flow (in this case, recursion).
All three appeal to me: the first two, from a pure mind-expanding, curiosity-whetting angle, and the third one from an engineering perspective. As programmers, we deal with a lot of abstractions, and useful abstractions make code easier to maintain, modify, and reuse.
The motivation behind Y Combinator may be best understood when we start with a vanilla example, and start putting some stringent constraints. Since, learning by doing is easily the best way to retain this material (together with writing about it), I not only wrote the code, but more importantly, did a dry run of the code by writing out the execution trace of the code for the simplest non-trivial case. I firmly believe that this builds intuition the best; sometimes even better than just writing the code and reasoning about it.
We start with the example of a recursive function which calculates the sum of numbers up to n. No special tricks here, just basic recursion.
(define sum-of-n (λ(n)
(cond
((zero? n) 0)
(else (+ n (sum-of-n (sub1 n))))
)
)
)
At an abstract level, a function like this is doing two things:
- Deciding what logic to execute at every step; here it decides that getting a zero as its argument means it should return zero, and for other numbers, it should add the current argument to the rest of the computation (with the recursive part)
- Recursing, i.e., the actual logic of continuing the computation. This part is independent of what logic the function encapsulates.
The reason I separate out the two aspects is this: let’s look at another function, the factorial:
(define factorial (λ(n)
(cond ((zero? n) 1)
(else (* n (factorial (sub1 n))))
)
))
If you examine this and the sum-of-n function, you will see that the recursion mechanic in both cases is the same, i.e., a call to itself with some new updated value. To drive the point home, look at the Fibonacci function, which has differently-shaped logic:
(define fib (λ(n)
(cond
((or (eq? n 1) (eq? n 2)) 1)
(else (+ (fib (sub1 n)) (fib (- n 2)))
)
)
))
The logic here is quite different: there are two recursive calls, and they are joined by addition. However, the mechanism for the recursive call stays the same: a call to the function itself, together with some (hopefully updated) value. The question that we’d like to pose is: Is there a way to decouple the mechanism of recursion from the logic of the recursive function? More concretely, we’d like to have the logic-specific function stick to deciding what calculations (loosely speaking) to run, and when it’s time to do the next step, it should not have hardcoded a call to itself (which is the recursion bit).
This is a slightly different way of imposing our first constraint, i.e., perform recursion assuming you are not allowed to refer to function names at all; more strictly, perform recursion on a function which does not have a name to begin with.
We will explore the solution by performing small refactoring steps, so that the path from named functions to the final result is traceable.
A simple path to the Y Combinator
This path, in my opinion, starts much more simply, and is how I got to the Y Combinator myself without constantly getting lost; thus mentally it is a superior path for me to remember the basis for the Y Combinator. Remember though, that this is how my own journey of understanding went: yours may very well differ, and I urge you to build your own mental model to understanding.
An infinite loop
First, we start with the infinite loop: it does nothing, and it runs forever.
(define infinite-loop (λ(step) (step step)))
(infinite-loop infinite-loop)
How does this work? Let’s trace it:
((λ(step) (step step)) infinite-loop)
=> (infinite-loop infinite-loop)
...
The second step ends up being the same as the original invocation, so it repeats ad infinitum.
Our task is two-fold:
- Make this infinite loop stop at some point
- Make this infinite loop do something useful
Making the infinite loop stop
Let’s start with the first task: making this infinite loop stop. We can simplify this task further, by making it stop immediately.
(define immediately-stopping-infinite (λ(step)
(cond
((eq? #t #t) 0)
(else (step step))
)
)
)
(immediately-stopping-infinite immediately-stopping-infinite)
The above stops immediately because it checks whether #t == #t (which is obviously true) and returns 0. The else branch, which would have taken the infinite loop path, is never invoked.
Making the infinite loop do something useful
We are now in a good position to build out our sum-of-n functionality straight away; the pieces are all there. The (eq? #t #t) is the placeholder for the recursion termination condition, and the else branch does the recursive part, in addition to adding to the current value. We need to add the n parameter to this function to carry the decrementing counter.
(define sum-of-n-1 (λ(step n)
(cond
((zero? n) 0)
(else (+ n (step step (sub1 n))))
)
)
)
(sum-of-n-1 sum-of-n-1 5)
We are almost at the point at which we can extract out the Y Combinator; technically, you can inline the invocation right here, and you’d have achieved your goal, but the Y Combinator requires some more mechanical refactoring. The next step is very mechanical: break up λ(step n) into two separate curried functions.
(define sum-of-n-curried
(λ(step)
(λ(n)
(cond
((zero? n) 0)
(else (+ n ((step step) (sub1 n))))
)
)
)
)
((sum-of-n-curried sum-of-n-curried) 5)
One of the things we are aiming for is for the sum-of-n function to not worry about the mechanics of recursion. In the above code, we see that evaluating (step step) and then applying it to (sub1 n) makes the whole thing look ugly. Therefore, we wish to inject (step step) into the λ(n). This means we “create” λ(n) by calling an intervening factory lambda which takes in a step-recur which is bound to a (step step) passed into the λ(n) factory lambda. The factory lambda returns the λ(n) function.
Loosely speaking, we are aiming for something like below:
sum-of-n-logic-factory = (λ(step-recur) (λ(n) ...(else (+ n (step-recur (sub1 n))))))
(sum-of-n-logic-factory (step step))
Written out properly without the sum-of-n-logic-factory name, we might write something like:
(define sum-of-n-explicit-3
(λ(step)
(
(λ(step-recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (step-recur (sub1 n))))
)
)
)
(step step)
)
)
)
However, the refactoring, as it stands, won’t work because it recurses forever, owing to eager evaluation of (step step).
Interlude: Why does it recurse?
Let’s evaluate the following to understand why it recurses forever:
((sum-of-n-explicit-3 sum-of-n-explicit-3) 1)
Let’s put in some shorthand, so that our eyes don’t bleed looking at all the parentheses. Assume that:
LADDER = (λ(step-recur) (λ(n) (cond ((zero? n) 0) (else (+ n (step-recur (sub1 n)))))))
so that we can write:
E = (λ(step) (LADDER (step step)))
STEP 1: We will now evaluate ((E E) 1).
STEP 2: So we need to evaluate (E E) first, let’s substitute the second E which is the parameter to step, so: step = E.
We unwrap one level of λ, and put E wherever step is, to get:
(E E) = (LADDER (E E))
STEP 3: To evaluate the above, we need to evaluate (E E). This is the same exact step as STEP 2, so we are now in an infinite recursion.
Delayed Evaluations vs. Infinity
One fix is to delay the evaluation of (step step), by wrapping it in a λ, evaluate it inside, like (step-recur), and then apply it to n-1. This works, but the sum-of-n logic has to remember to evaluate the λ, then apply it.
(define sum-of-n-explicit-no-arg-recur
(λ(step)
(
(λ(step-recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n ((step-recur) (sub1 n))))
)
)
)
(λ() (step step))
)
)
)
To make the function oblivious of worrying about evaluating step-recur and then applying it to n-1, we can enhance the (λ() (step step)) lambda to also take in the n-1 parameter and evaluate it. Now the sum-of-n logic just assumes the recursion lambda is a plain function it can just apply on n-1.
(define sum-of-n-explicit-1-arg-recur
(λ(step)
(
(λ(step-recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (step-recur (sub1 n))))
)
)
)
(λ(n) ((step step) n))
)
)
)
Note that the extra brackets around step-recur are no longer present because we do not need to evaluate + apply inline: the evaluation happens inside step-recur itself. Loosely speaking, we now have (in some Scheme-like pseudocode):
sum-of-n-logic-factory = (λ(step-recur) (λ(n) ...(else (+ n (step-recur (sub1 n))))))
(sum-of-n-logic-factory (λ(n)((step step) n)))
Interlude: Why does this not recurse infinitely?
Let’s verify that this does NOT recurse. The simple, if somewhat blase, explanation is given first. After that, we trace through a simple but nontrivial execution to solidify our intuition.
Let’s evaluate (E E) again.
E=(λ(step) (LADDER (λ(n) ((step step) n))))
LADDER is the same, but we get a different result, on substitution:
(E E) = (LADDER (λ(n) ((E E) n)))
The expression (λ(n) ((E E) n)) does not get evaluated right then, since it is a lambda expression, in contrast to (E E), which Scheme would have attempted to evaluate straight away. For completeness’ sake, note that the original version would have worked in Haskell, because Haskell has lazy evaluation (see References).
What is more instructive is to see the trace of what happens through a complete execution. We will use 2 as the input parameter, because that exercises both halves of the sum-of-n logic.
Let E = sum-of-n-explicit-1-arg-recur, then evaluate ((E E) 2). Then, substituting E everywhere step is, we get:
((λ(step-recur) (λ(n) (cond ((zero? n) 0) (else (+ n (step-recur (sub1 n))))))) (λ(n) ((E E) n) ))
=> P = (E E) = (λ(n) (cond ((zero? n) 0) (else (+ n ( (λ(n) ((E E) n) ) (sub1 n))))))
Let’s call P(2):
=> P(2) = (cond ((zero? 2) 0) (else (+ 2 ( (λ(n) ((E E) n) ) (sub1 2)))))
=> P(2) = (+ 2 ( (λ(n) ((E E) n) ) 1))
=> P(2) = (+ 2 ( (E E) 1))
=> P(2) = (+ 2 ( (λ(n) (cond ((zero? n) 0) (else (+ n ( (λ(n) ((E E) n) ) (sub1 n)))))) 1))
=> P(2) = (+ 2 (cond ((zero? 1) 0) (else (+ 1 ((λ(n) ((E E) n)) (sub1 1)))))
=> P(2) = (+ 2 (cond ((zero? 1) 0) (else (+ 1 ((λ(n) ((E E) n)) 0)))))
=> P(2) = (+ 2 (+ 1 ((λ(n) ((E E) n)) 0)))
=> P(2) = (+ 2 (+ 1 ((E E) 0)))
=> P(2) = (+ 2 (+ 1 ( (λ(n) (cond ((zero? n) 0) (else (+ n ( (λ(n) ((E E) n) ) (sub1 n)))))) 0)))
=> P(2) = (+ 2 (+ 1 (cond ((zero? 0) 0) (else (+ 0 ( (λ(n) ((E E) n) ) (sub1 0)))))))
=> P(2) = (+ 2 (+ 1 0)) = 3
Expansion: The Attack of the Parentheses
You can run ((sum-of-n-explicit-1-arg-recur sum-of-n-explicit-1-arg-recur) 10) for the above. Now that we have (hopefully) convinced ourselves that this does work, let’s take stock of where we are.
We have a function called sum-of-n-explicit-1-arg-recur which looks like:
(define sum-of-n-explicit-1-arg-recur
(λ(step)
((λ(step-recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (step-recur (sub1 n))))
)
)
) (λ(n) ((step step) n))
)
)
)
The easy way to think about this function is as one function with three arguments, each of which answers a separate question related to recursion:
step: What is the next recursive function to call? In this case, the answer is “itself”, i.e.,sum-of-n-explicit-1-arg-recur.step-recur: How is the recursion invoked, givenstep? In this case it is((step step) n), where(step step)is basically the unchecked infinite loop, andnis the recursion state.n: The recursion state which is used for the logic of the recursion, and to determine the termination condition of the recursion. In this case, it is a single number, but it can be anything that represents the state of the computation at each level of the recursion.
Everything else is just refactoring and currying to make the whole thing into a reusable pattern.
We can call it like:
((sum-of-n-explicit-1-arg-recur sum-of-n-explicit-1-arg-recur) 10)
As you can see, we are still using names. As the next step, if we go in and blindly expand sum-of-n-explicit-1-arg-recur into its body, we get the following:
(
(λ(step) ((λ(step-recur) (λ(n) (cond ((zero? n) 0) (else (+ n (step-recur (sub1 n)))))))(λ(n) ((step step) n))))
(λ(step) ((λ(step-recur) (λ(n) (cond ((zero? n) 0) (else (+ n (step-recur (sub1 n)))))))(λ(n) ((step step) n))))
)
We are actually very close to the end, because, if you see the above definition, you will see that there are no references to any unbound names. The only references present are those of the bound arguments.
Let’s refactor by pulling out the sum-of-n logic, which is the thing which differs in recursive walks; the recursion itself is always a standard pattern.
(define sum-of-n-step-factory
(λ(step-recur) (λ(n) (cond ((zero? n) 0) (else (+ n (step-recur (sub1 n)))))))
)
(define sum-of-n-explicit-5
(
(λ(step) (sum-of-n-step-factory (λ(n) ((step step) n))))
(λ(step) (sum-of-n-step-factory (λ(n) ((step step) n))))
)
)
sum-of-n-step-factory is a hardcoded step function in sum-of-n-explicit-5; it is called a step function because it returns a function which tells us how to take the next step; this can be either termination or further recursion. We can make the above more general by passing a (generic) step function into a factory function. This factory function is the Y Combinator.
(define y-combinator (λ(step-fn-factory)
(
(λ(step) (step-fn-factory (λ(n) ((step step) n))))
(λ(step) (step-fn-factory (λ(n) ((step step) n))))
)
))
We can call this like:
((y-combinator sum-of-n-step-factory) 10)
Note what we have done: the sum-of-n-step-factory knows nothing about recursion. All it gets is a step-recur lambda which it invokes when it needs to run the next step; from that angle, it contains only the program logic, not control flow logic. The Y Combinator knows nothing about the program logic, whether it be factorial, Fibonacci, or something completely different; all it knows is to take the program logic lambda, and convert it into a recursive lambda which fuses the program logic and control flow (in this case, recursion).
An Alternative Path: Refactoring the Total Function
For comparison, here is a second route to the same point, which starts from a more conventional refactoring instead of from an infinite loop.
One of the first things we usually reach for, when attempting to hide implementation details from a function, is to have it simply call another function that has been passed in. This by itself, does not solve the problem, but it is a start. Accordingly, we pass in a lambda next that sum-of-n calls instead of invoking itself for recursion. But let’s take a smaller step: pass in a next which is not used at all, and thus, is effectively an unused argument, like so:
(define sum-of-n-taking-next (λ(next n)
(cond
((zero? n) 0)
(else (+ n (sum-of-n-taking-next next (sub1 n))))
)
)
)
We can call this function like (sum-of-n-taking-next (λ() '()) 3), and this will still work. Any next we pass in would be fine, because we have not even attempted to use it in any capacity (yet).
Let’s try using next, for recursion. For that we need to invoke next as the recursive lambda instead of directly calling sum-of-n-taking-next.
(define sum-of-n-taking-next-no-ref (λ(next n)
(cond
((zero? n) 0)
(else (+ n (next next (sub1 n))))
)
)
)
This makes sense, but we need to talk about how we invoke such a method:
(sum-of-n-taking-next-no-ref sum-of-n-taking-next-no-ref 10)
This should have the same desired effect because the next that we are passing in, is the original lambda itself (in this case, sum-of-n-taking-next-no-ref).
For compactness, let’s put the body in a single line. This ends up being a lot more parentheses on a single line, but we will not need to parse all of the details on every line.
(define sum-of-n-explicit-1
(λ(next n) (cond ((zero? n) 0) (else (+ n (next next (sub1 n)))))))
The next small refactoring: let’s break up the outer lambda into two separate curried functions.
(define sum-of-n-explicit-2
(λ(next) (λ(n) (cond ((zero? n) 0) (else (+ n ((next next) (sub1 n))))))))
We can call this using ((sum-of-n-explicit-2 sum-of-n-explicit-2) 10). No real difference, it’s just that the call is explicitly sequentialised. Remember that this is how we will continue to invoke the recursion: it’s an important invariant in every step of the refactoring.
Apart from calling its argument next instead of step, this is the same function as sum-of-n-curried from the simple path.
Let’s break out (next next) into the parameterised recur, like we did earlier:
(define sum-nn-2 (λ(next)
(λ(recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (recur (sub1 n))))
)
)
)(λ(n) ((next next) n))
)
)
Keep in mind that we are still invoking the computation using ((sum-nn-2 sum-nn-2) 10).
Next, we perform an explicit refactoring of pulling out the following into a logique variable:
(λ(recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (recur (sub1 n))))
)
)
)
so that we get the following setup:
(define logique
(λ(recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (recur (sub1 n))))
)
)
)
)
(define sum-nn-3 (λ(next)
(logique (λ(n) ((next next) n)))
)
)
This can be invoked the same way as before, i.e., ((sum-nn-3 sum-nn-3) 10). Note that it might be tempting to try ((sum-nn-3 logique) 10), but this will not work. For n=1, this works, but only by accident.
((logique (λ(n) ((logique logique) n))) 1)
= (+ 1 ((λ(n) ((logique logique) n)) 0))
= (+ 1 ((logique logique) 0))
= (+ 1 0)
= 1
But watch what happens for n=2 and above.
((logique (λ(n) ((logique logique) n))) 2)
= (+ 2 ((λ(n) ((logique logique) n)) 1))
= (+ 2 ((logique logique) 1))
= (+ 2 (+ 1 (logique 0))) ;; ERROR: `(logique 0)` is a function and not a number that can be added!
So, remember the invariant that the call is always invoked as ((sum-nn-3 sum-nn-3) 10). This is simply a consequence of how we started, and the refactorings preserve that invocation behaviour.
Now, sum-nn-3 uses the external definition of logique, so we can wrap sum-nn-3 in another lambda which takes in this logique explicitly as an argument. Let’s call this wrapping lambda xxx arbitrarily. I have explicitly left the name sum-nn-3 as a let binding to reinforce that the way to invoke this form is (((xxx logique) (xxx logique)) 10), since sum-nn-3 is now (xxx logique).
(define xxx (λ(logic)
(let ((sum-nn-3 (λ(next)
(logic (λ(n) ((next next) n)))
)
))
sum-nn-3
)
)
)
The last step that we can perform is simply invoke ((xxx logique) (xxx logique)) directly inside xxx, so that the only unbound value remaining is n. Then we get the following:
(define xxx (λ(logic)
(let ((sum-nn-3 (λ(next)
(logic (λ(n) ((next next) n)))
)
))
(sum-nn-3 sum-nn-3)
)
)
)
We can now rename sum-nn-3 to something more generic, say, step, and write:
(define y-combinator (λ(logic)
(let ((step (λ(next)
(logic (λ(n) ((next next) n)))
)
))
(step step) ;; Trigger the construction of the function
)
)
)
Inlining the definition of step, we get the familiar Y-combinator:
(define y-combinator (λ(logic)
((λ(next) (logic (λ(n) ((next next) n))))
(λ(next) (logic (λ(n) ((next next) n))))
)
)
)
Conclusion
Note that we started from two different points:
- Plain empty recursion, and we added the logic incrementally, then refactored
- Full fledged recursive function with the logic, and we refactored out the useful parts.
In both cases, we end up with the Y-Combinator.
Y-Combinator with names
For completeness, let’s allow names, and see what an equivalent construction of the Y-Combinator looks like. Start with your usual sum-of-n:
(define sum-of-n
(λ(n)
(cond
((zero? n) 0)
(else (+ n (sum-of-n (sub1 n))))
)
)
)
We still want the same shape of our step function, so the first thing to do is pull out the recur, like we have already done:
(define sum-of-n
(λ(L)
(λ(n)
(cond
((zero? n) 0)
(else (+ n ((L L) (sub1 n))))
)
)
)
)
We invoke this by ((sum-of-n sum-of-n) 10). Now, since we can use names, let’s define a function ‘mk-recur’, like so:
(define mk-recur
(λ(L)
(λ(n)
((L L) n)
)
)
)
So now, we might want to use this like so:
(define sum-of-n
(λ(recur)
(λ(n)
(cond
((zero? n) 0)
(else (+ n (recur (sub1 n))))
)
)
)
)
((sum-of-n (mk-recur sum-of-n)) 1)
This won’t work beyond n=1, though. The reason is that, in the original version, (L L) is built up in every recursion level, whereas in the mk-recur version, (L L) is built only once. For n=1, recur is (L L), which is fine. But look at what is called for any level deeper than 1:
n=1:recur=(λ(n) ((L L) n)), the recursive call is(+ 1 ((L L) 0)) = (+ 1 0) = 1. This is fine.
n=2:recur=(λ(n) ((L L) n)), the recursive call is(+ 2 ((L L) 1)).recur=L, the recursive call is(+ 1 (L 0)).(L 0)is a function, so we get(+ 1 <function>), which is an error.
Basically, the shape of recur does not maintain the (L L) shape at every recursion level. In fact, there is no generalised recursion happening at all. To maintain the recursive shape of recur, we need to make sure that mk-recur keeps getting called at every recursion level, so that we can keep supplying the first argument of L every time. We can do it like so:
(define mk-recur
(λ(L)
(λ(n)
((L (mk-recur L)) n)
)
)
)
Now, for n=2, we get the following trace (I have shortened sum-of-n to L for readability):
((L (mk-recur L)) 2)
= ((L (λ(n) ((L (mk-recur L)) n))) 2)
= (+ 2 ((λ(n) ((L (mk-recur L)) n)) 1))
= (+ 2 ((L (mk-recur L)) 1))
= (+ 2 ((L (λ(n) ((L (mk-recur L)) n))) 1))
= (+ 2 (+ 1 ((λ(n) ((L (mk-recur L)) n) ) 0)))
= (+ 2 (+ 1 ((L (λ(n) ((L (mk-recur L)) n))) 0)))
= (+ 2 (+ 1 0))
= 3
The Y-Combinator then is just:
(define Y!
(λ(L)
(L (mk-recur L))
)
)
which we can use like so:
((Y! sum-of-n) 10)
Y-Combinator with names and mutation
[TODO] This is the least favourite part of Y-Combinators for me, because it involves mutation.