pmb: (kitty behind printer)
[personal profile] pmb
def voodoo(f):
    def A(x):
        def B(y):
            return x(x)(y)
        return f(B)
    return A(A)

def hypothetical_fact(factorial):
    def f(n):
        if n == 0:
            return 1
        else:
            return n * factorial(n-1)
    return f

fact = voodoo(hypothetical_fact)

print fact(30)
The Z combinator is messed up. Anybody want to explain the above code to me?

Re: I'll give it a shot...

Date: 2005-04-01 05:12 pm (UTC)
From: [identity profile] stereotype441.livejournal.com
Hmm... On second reading, I don't think I got that second paragraph exactly right. Instead of saying "But it does it without using any recursive functions" I should have said "But it does it without using any self-referential functions". Oops.

Profile

pmb: (Default)
pmb

October 2009

S M T W T F S
    1 23
45678910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 15th, 2026 02:18 pm
Powered by Dreamwidth Studios