Discussion attendance

Question 1

Fill in the blanks (without using any numbers in the first blank!) to get the expected output.

>>> (lambda x: lambda y: ________)(___)(lambda z: z*z)()
9

Toggle Solution

>>> (lambda x: lambda y: lambda: y(x))(3)(lambda z: z*z)()
9

Question 2

Draw the environment diagram resulting from running the following code:

x = 6
def dread(pirate):
    x = 10
    def roberts(westley):
        x = 2000
        return westley + pirate(x)

    return roberts(x)

dread(lambda spot: x + spot)

Toggle Solution


Question 3

Suppose we’ve already entered the following code into the Python interpreter:

wild = 2

def pig(out):
    while out < 10:
        if out % 2:
            print('oink')
        if out % 3:
            print('squeal')

        def x(x):
            return out + x

        out = out * out
    return x(out)

def hog(wild):
    while pig(wild):
        wild = wild // 3

        if wild % 3 == 0:
            return pig(wild + 3)

def swine(hog):
    return hog(wild) + wild

What would Python print for each of the following lines?

>>> pig(3)

>>> y = hog(7)

>>> hog(swine(hog))

>>> swine(pig)

Toggle Solution

>>> pig(3)
oink
oink
162

>>> y = hog(7)
oink
squeal
squeal
squeal
oink
oink

>>> hog(swine(hog))
squeal
squeal
oink
oink
Error

>>> swine(pig)
squeal
squeal
34