【发布时间】:2018-02-17 02:52:42
【问题描述】:
给了我一个函数列表,并要求我用add1 和repeated 定义plus(x,y)。 plus 是一个接受两个数字并返回总数的函数。但是,我的定义无法获得任何输出。它只是给出函数的名称。任何帮助表示赞赏!
add1 = lambda x: x + 1
def compose(f, g):
return lambda x: f(g(x))
def repeated(f, n):
if n == 0:
return lambda x: x
else:
return compose(f, repeated(f, n - 1))
def plus(x, y):
return repeated(add1, y)
【问题讨论】:
-
你能展示一下你是如何使用这些函数的吗?
-
x在plus中用在哪里? -
测试用例为 plus() 任意两个整数,plus(1,3), plus(2,5) 等。
标签: python higher-order-functions