I couldn't understand the Y-combinator, so I tried to implement a function that enabled recursion without native implementation. After some thinking, I ended up with this:
Y = λx.(λv.(x x) v)
Which is shorter than the actual one:
Y = λf.(λx.f (x x)) (λx.f (x x))
And, for my surprise, worked. Some examples:
// JavaScript
Y = function(x){
return function(v){
return x(x, v);
};
};
sum = Y(function(f, n){
return n == 0 ? 0 : n + f(f, n - 1);
});
sum(4);
; Scheme
(define Y (lambda (x) (lambda (v) (x x v))))
(define sum (Y
(lambda (f n)
(if (equal? n 0)
0
(+ n (f f (- n 1)))))))
(sum 4)
Both snippets output 10 (summation from 0 to 4) as expected.
What is this, why it is shorter and why we prefer the longer version?
x
tox
, instead of passing the application ofx
tov
tox
. What you've implemented isY = λx.(λv.(x x) v)
. – Crazyweed