On Sun, 09 Oct 2011 02:25:27 +0200, Alexander Kapps wrote:

> Even if it's off-topic, could you add some similar explanations for 
> Church numerals (maybe Lambda calculus it isn't too much?)

The Church numeral for N is a function of two arguments which applies its
first argument N times to its second, i.e. (f^N)(x) = f(f(...(f(x))...)).

IOW:

def zero(f, x):
    return x

def one(f, x):
    return f(x)

def two(f, x):
    return f(f(x))

def three(f, x):
    return f(f(f(x)))

And so on.

In general:

def applyN(n, f, x):
    for i in xrange(n):
        x = f(x)
    return x

def church(n):
    return lambda f, x: applyN(n, f, x)

seven = church(7)       # this is the Church numeral for 7

> seven(lambda x: x + 1, 0)
7
> seven(lambda x: x * 2, 1)
128
> seven(lambda x: x + ".", "")
'.......'


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to