Skip to main content

Scheme: My Y Combinator

I've been reading The Little Schemer, and it posed an interesting question: how can you create a recursive function without having the ability to "define" a name for it? For instance, in Scheme, how can you create a simple, recursive function to calculate the length of a list without having the ability to use "define"?

Here's my approach:
((lambda (my-length l)
(my-length my-length l))
(lambda (my-length l)
(cond
((null? l) 0)
(else (add1 (my-length my-length (cdr l))))))
'(1 2 3 4 5))
The outer function is "(lambda (my-length l) ...)". It takes a reference to a function that it calls my-length. It calls that function "(my-length my-length l)". Hence, that function, which I call "my-length", receives the list as well as a reference to itself, "(lambda (my-length l) ...)". Since it receives a reference to itself, it's able to call itself recursively.

It turns out the real answer is called the Y Combinator. It's written as:
Y = λf·(λx·f (x x)) (λx·f (x x))
It was created by Haskell Curry. To use the Y Combinator to solve the above problem, you'd write:
((lambda (le)
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(le (lambda (x)
((mk-length mk-length) x))))))
(lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (length (cdr l)))))))
Notice that my version is shorter, but the Y Combinator version is more elegant because the actual length function isn't burdened with having to recursively pass a reference to itself.

If you didn't understand any of the above, don't sweat it. I read it twice, and I still barely get it ;) However, I think I now understand why Paul Graham used that name for his startup incubator, Y Combinator. Paul Graham is a Lisp guy, and his Y Combinator is a startup meant to recursively launch other startups ;)

Comments

Unknown said…
Y = lambda f: (lambda g: f (lambda h: g(g)(h))) \
(lambda g: f (lambda h: g(g)(h)))

fact = lambda f: lambda n: 1 if n == 0 else n * f(n-1)

print Y(fact)(100)
jjinux said…
> slobin said...

You said "Y =" and "fact =". I think I said, "how can you create a recursive function without having the ability to "define" a name for it?" I'm assuming you can't make any definitions.
Anonymous said…
The problem is not "naming" by itself (after all, naming is just a handy shortcut), but using the name inside it's own definition. If you insist on reading your conditions literally, just add parentheses around each of two expressions, and concatenate them:

>>> (lambda f: (lambda g: f (lambda h: g(g)(h)))(lambda g: f (lambda h: g(g)(h))
))(lambda f: lambda n: 1 if n == 0 else n * f(n-1))(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915
608941463976156518286253697920827223758251185210916864000000000000000000000000L

(Sorry for the long line, it is a copy of real python session ;-). The Y combinator is inside the first outermost pair of parentheses, the factorial definition is inside the second, and the numeric argument is inside the third. In my original comment this is just a bit more readable, but the internal mechanic is the same.
Anonymous said…
P.S. And, by the way, the analog of your first solution is also possible:

(lambda f,n: f(f,n))(lambda f,n: 1 if n < 2 else n*f(f,n-1),100)

And I do 100% agree with you that "combinator version is more elegant" albeit textually longer.

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from