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:
It turns out the real answer is called the Y Combinator. It's written as:
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 ;)
Here's my approach:
((lambda (my-length l)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.
(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))
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)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.
((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)))))))
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
(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)
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.
>>> (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.
(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.