Skip to content

Instantly share code, notes, and snippets.

@nitin-motiani
nitin-motiani / y-comb.lisp
Created April 13, 2012 19:56
Y combinator in common lisp
(setf Y (lambda (g)
(funcall (lambda (f) (lambda (n) (funcall (funcall g (funcall f f)) n)))
(lambda (f) (lambda (n) (funcall (funcall g (funcall f f) n)))))))
(setf factorial (funcall Y
(lambda (f)
(lambda (n)
(if (< n 2) n
(* n (funcall f (- n 1))))))))
@nitin-motiani
nitin-motiani / y-comb.js
Created April 13, 2012 20:55
Y combinator in JavaScript
var Y = function(g) {
var recur = function (f) {
return function(n) {
return g(f(f))(n);
};
};
return recur(recur);
};
var fact = function(rec) {
@nitin-motiani
nitin-motiani / y-comb.py
Created April 13, 2012 20:57
Y combinator in python
def Y (g):
r = lambda f : lambda n : g(f(f))(n)
return r(r);
def fact (f) :
return lambda n : n if n < 2 else n * f(n - 1)
factorial = Y(fact)
print(factorial(7))