Lambda Functions

Lambda at Last (fa14-mt1-4)

  1. Fill in the blank below with an expression so that the second line evaluates to 2014. You may only use the names two_thousand, two, k, four, and teen and parentheses in your expression (no numbers, operators, etc.)

    two_thousand = lambda two: lambda k: 
    two_thousand(7)(lambda four: lambda teen: 2000 + four + teen)
  2. The if_fn returns a two-argument function that can be used to select among alternatives, similar to an if statement. Fill in the return expression of factorial so that it is defined correctly for non-negative arguments. You may only use the names if_fn, condition, a, b, n, factorial, base, and recursive and parentheses in your expression (no numbers, operators, etc.).

    def if_fn(condition):
      if condition:
        return lambda a, b: a
      else:
        return lambda a, b: b
    
    def factorial(n):
      """
      Compute N! for non-negative N. N! = 1 * 2 * 3 * ... * N.
    
      >>> factorial(3)
      6
      >>> factorial(5)
      120
      >>> factorial(0)
      1
      """
    
      def base():
        return 1
    
      def recursive():
        return n * factorial(n-1)
    
      return 

You Complete Me (sp15-mt1-3)

Add parentheses and single-digit integers in the blanks below so that the expression on the second line evaluates to 2015. You may only add parentheses and single-digit integers. You may leave some blanks empty.

lamb = lambda lamb: lambda: lamb + lamb
lamb(1000)__________ + (lambda b, c: b__________ * b__________ - c__________)(lamb(__________), 1)__________

Multikarp (fa16-mt1-5)

Terminology. An order 1 numeric function is a function that takes a number and returns a number. An order 2 numeric function is a function that takes a number and returns an order 1 numeric function. Likewise, an order n numeric function is a function that takes a number and returns an order n − 1 numeric function.

The argument sequence of a nested call expression is the sequence of all arguments in all subexpressions, in the order they appear. For example, the expression f(3)(4)(5)(6)(7) has the argument sequence 3, 4, 5, 6, 7.

  1. Implement multiadder, which takes a positive integer n and returns an order n numeric function that sums an argument sequence of length n.

    def multiadder(n):
      """
      Return a function that takes N arguments, one at a time, and adds them.
    
      >>> f = multiadder(3)
      >>> f(5)(6)(7)
      18
      >>> multiadder(1)(5)
      5
      >>> multiadder(2)(5)(6)
      11
      >>> multiadder(4)(5)(6)(7)(8) # 5 + 6 + 7 + 8
      26
      """
    
      assert n > 0
      if :
        return 
      else:
        return 
  2. Complete the expression below by writing one integer in each blank so that the whole expression evaluates to 2016. Assume multiadder is implemented correctly.

    def compose1(f, g):
      def h(x):
        return f(g(x))
      return h
    
    compose1(multiadder(__________)(1000), multiadder(__________)(10)(__________))(1)(2)(3)