I am currently in an intro to python and computational theory class, and there was recently a difficult question on the midterm that I simply was not able to solve. It involves writing code for a program that adds numbers. I believe the question is supposed to use recursion. I don't remember how exactly the question was worded but here is the basic idea.
>>> multi_three = multiadder(3)
assert n > 0
if _________________________ :
def multiadder(n): if n <= 1: return lambda t: t else: return lambda a: lambda b: multiadder(n-1)(a+b) if __name__ == '__main__': print(multiadder(5)(1)(2)(3)(4)(5))
n == 1, the result must be a function returning the input.
n > 1, wrap the result of
n-1, by adding input.
This also works for concatenating strings, and other accumulating operations:
>>> multiadder(3)('p')('q')('r') pqr