Y-Combinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# a functional is a function that takes a function for its input | |
def fact(factorial): | |
def fn(n): | |
if n == 0: return 1 | |
else: | |
return n * factorial(n - 1) | |
return fn | |
def fib1(fib): | |
def fn(n): | |
if n == 0: return 0 | |
if n < 2: return 1 | |
else: | |
return fib(n - 2) + fib(n - 1) | |
return fn | |
# Y = λf.(λx.f (x x)) (λx.f (x x)) | |
# Computes the fixed point of a functional, Y(F) = F(Y(F)) | |
def Y(f): | |
def inner(cc): | |
return f(lambda x: cc(cc)(x)) | |
return inner(inner) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment