"""Submission for 61A Homework 2. Name: Login: Collaborators: """ # Q1 def product(n, term): """Return the product of the first n terms in a sequence. term -- a function that takes one argument """ "*** YOUR CODE HERE ***" def factorial(n): """Return n factorial by calling product. >>> factorial(4) 24 """ "*** YOUR CODE HERE ***" # Q2 def accumulate(combiner, start, n, term): """Return the result of combining the first n terms in a sequence. *** YOUR DESCRIPTION HERE *** """ "*** YOUR CODE HERE ***" def summation_using_accumulate(n, term): """An implementation of summation using accumulate.""" "*** YOUR CODE HERE ***" def product_using_accumulate(n, term): """An implementation of product using accumulate.""" "*** YOUR CODE HERE ***" # Q3 def double(f): """Return a function that applies f twice. f -- a function that takes one argument """ "*** YOUR CODE HERE ***" # Q4 def repeated(f, n): """Return the function that computes the nth application of f. f -- a function that takes one argument n -- a positive integer >>> repeated(square, 2)(5) 625 """ "*** YOUR CODE HERE ***" def square(x): """Return x squared.""" return x * x def compose1(f, g): """Return a function h, such that h(x) = f(g(x)).""" def h(x): return f(g(x)) return h # Q5 (Extra) def zero(f): """Church numeral 0.""" return lambda x: x def successor(n): return lambda f: lambda x: f(n(f)(x)) def one(f): """Church numeral 1.""" "*** YOUR CODE HERE ***" def two(f): """Church numeral 2.""" "*** YOUR CODE HERE ***" def add_church(m, n): """Return the Church numeral for m + n, for Church numerals m and n.""" "*** YOUR CODE HERE ***" def church_to_int(n): """Convert the Church numeral n to a Python integer. >>> church_to_int(zero) 0 >>> church_to_int(one) 1 >>> church_to_int(two) 2 >>> church_to_int(add_church(two, two)) 4 """ "*** YOUR CODE HERE ***"