g****t 发帖数: 31659 | 1 我从未写过一行Continuation。
我理解的Turing的1936 paper数理逻辑知识,能不能落到实处?刚才花了一小时python。
作为一个无名散户,我愿意把自己的错误过程,polish的过程分享给大家批评。
这程序的目的是不用loop index把一个函数g调用n次。
实现用continuation消除loop的转换。
我想我证实了,从Turing 1936 paper出发,一般人都可以"invent" continuation。
def g(x):
print(x+1)
return x+1
def F(n,S):
if n == 0:
t = S(0)
else:
F(n-1,( lambda x:S(S(x)) ))
#F(3,g) called g 2^x times
def F1(n,S):
if n == 0:
t = S(0)
else:
f = lambda x:g(x)
# we had to use g that is not in the F1's body and it looks not nice
F1(n-1,(lambda x: f(S(x) ) ))
def F2(n,S,h):
if n == 0:
t = S(0)
else:
f = lambda x:h(x)
#h is used to keep the external information
F2(n-1,(lambda x: f(S(x) ) ),h)
#F2(3,g,g)
def F3(n,S_name):
F2(n,eval(S_name),eval(S_name))
F3(8,'g')
# test, should print 1,2,...,9 |
|