Y-combinator in Racket supporting variant arguments

First Post:

Blog Link:

Y-combinator:

1
2
3
4
5
6
(define Y
(lambda (g)
(let ((d (lambda (f)
(lambda args
(apply (g (f f)) args)))))
(d d))))

An example:

1
2
3
4
5
6
(let ((fact (Y (lambda (fact)
(lambda (n)
(if (zero? n)
1
(* n (fact (- n 1)))))))))
(fact 10))

Another example:

1
2
3
4
5
6
(let ((gcd (Y (lambda (gcd)
(lambda (a b)
(if (zero? b)
a
(gcd b (remainder a b))))))))
(gcd 12 8))