discussion 2:https://inst.eecs.berkeley.edu/~cs61a/sp21/disc/disc02/
目录HOF
# Q1: Keep Ints
def keep_ints(cond, n):
"""Print out all integers 1..i..n where cond(i) is true
>>> def is_even(x):
... # Even numbers have remainder 0 when divided by 2.
... return x % 2 == 0
>>> keep_ints(is_even, 5)
2
4
"""
"*** YOUR CODE HERE ***"
i = 1
while i <= n:
if cond(i):
print(i)
i += 1
# Q2: (Tutorial) Make Keeper
def make_keeper(n):
"""Returns a function which takes one parameter cond and prints out all integers 1..i..n where calling cond(i) returns True.
>>> def is_even(x):
... # Even numbers have remainder 0 when divided by 2.
... return x % 2 == 0
>>> make_keeper(5)(is_even)
2
4
"""
"*** YOUR CODE HERE ***"
def do_keep(cond):
i = 1
while i <= n:
if cond(i):
print(i)
i += 1
return do_keep
Environment Diagram
python tutor:http://pythontutor.com/composingprograms.html#mode=edit
# Q3: Curry2 Diagram
def curry2(h):
def f(x):
def g(y):
return h(x, y)
return g
return f
make_adder = curry2(lambda x, y: x + y)
add_three = make_adder(3)
add_four = make_adder(4)
five = add_three(2)
# Q4: Curry2 Lambda
"*** YOUR CODE HERE ***"
# curry2 = lambda h: lambda x: lambda y: h(x, y)
# Q5: (Tutorial) HOF Diagram Practice
n = 7
def f(x):
n = 8
return x + 1
def g(x):
n = 9
def h():
return x + 1
return h
def f(f, x):
return f(x + n)
f = f(g, n)
g = (lambda y: y())(f)
# Q6: YY Diagram
y = "y"
h = y
def y(y):
h = "h"
if y == h:
return y + "i"
y = lambda y: y(h)
return lambda h: y(h)
y = y(y)(y)
Self Reference
注意:以下函数返回函数时,会返回真实地址,可直接在网站上运行
在课程网站上直接返回
# Q7: (Tutorial) Warm Up: Make Keeper Redux
def make_keeper_redux(n):
"""Returns a function. This function takes one parameter <cond> and prints out
all integers 1..i..n where calling cond(i) returns True. The returned
function returns another function with the exact same behavior.
>>> def multiple_of_4(x):
... return x % 4 == 0
>>> def ends_with_1(x):
... return x % 10 == 1
>>> k = make_keeper_redux(11)(multiple_of_4)
4
8
>>> k = k(ends_with_1)
1
11
>>> k
<function do_keep>
"""
# Paste your code for make_keeper here!
def do_keep(cond):
i = 1
while i <= n:
if cond(i):
print(i)
i += 1
return make_keeper_redux(n)
return do_keep
# Q8: Print Delayed
def print_delayed(x):
"""Return a new function. This new function, when called, will print out x and return another function with the same behavior.
>>> f = print_delayed(1)
>>> f = f(2)
1
>>> f = f(3)
2
>>> f = f(4)(5)
3
4
>>> f("hi") # a function is returned
5
<function delay_print>
"""
def delay_print(y):
print(x)
return print_delayed(y)
return delay_print
# Q9: (Tutorial) Print N
def print_n(n):
"""
>>> f = print_n(2)
>>> f = f("hi")
hi
>>> f = f("hello")
hello
>>> f = f("bye")
done
>>> g = print_n(1)
>>> g("first")("second")("third")
first
done
done
<function inner_print>
"""
def inner_print(x):
if n <= 0:
print("done")
else:
print(x)
return print_n
return inner_print