新农上路
- 积分
- 97
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-8-26
- 最后登录
- 1970-1-1
|
本帖最后由 lh6210 于 2015-11-3 19:37 编辑
lab02: Higher order functions and lambdas
Question 1:
t = lambda f: lambda x:f(f(f(x)))
s = lambda x: x+1
求: t(s)(0)
解:
t(s)= lambda x: s(s(s(x)))
t(s)(0) = s(s(s(0))) =3
bar = lambda y: lambda x: pow(x,y)
求bar()(15)
解: missing one argument
foo = lambda :32
foobar= lambda x,y:x//y
a= lambda x: foobar(foo(), bar(4)(x))
求a(2)
解:a(2)= foobar( foo(), bar(4)(2))
= foobar(32, pow(4,2))
= foobar (32, 16)
= 32//16=2
b = lambda x,y: print('summer')
c=b(4,'dog')
求print(c)
解:
c = None, 显示为 summer
print(c) 即 print(None) = None
Question 2:
a = lambda x: x*2 +1
def b(b,x):
return b(x + a(x))
x=3
求 b(a, x)
解:
b(a, x) will return a(x + a(x)) = a(3 + a(3)) =21
Question 3:
def first(x):
x +=8
def second(y):
print('second')
return x+y
print('first')
return second
求 f= first(15)
解:
print('first') 显示 first
求f
解:
function <second> ...
求f(16)
解:
second
39
def even(f):
def odd(x):
if x<0:
return f(-x)
return f(x)
return odd
stevphen = lambda x:x
stewart = even(stevphen)
求 stewart, stewart(61), stewart(-4)
解:
stewart: function <odd> ...
stewart(61): 61
stewart(-4) : 4
Question 4:
Define a function make_derivative that returns a function: the derivative of a function f.
def make_derivative(f, h=1e-5):
....
return g
assuming that f is a single-variable mathematical function, its derivative will also be a single-variable function. When called with a number a, the derivative will estimate the slope of f at point (a, f(a)).
g(a) = (f(a+h) - f(a))/a (h = 1e-5)
解:
def make_derivative(f, h=1e-5):
def g(a):
return (f(a+h) - f(a))/h
return g
Question 5:
from operator import add , sub
def caesar_generator(num, op):
def f(x):
return num_to_letter(op(letter_to_num(x), num))
return f
等同于 return lambda x: num_to_letter(op(letter_to_num(x), num)
Question 6:
def make_encrypter(f1, f2, f3):
f1, f2, f3 = looper(f1), looper(f2), looper(f3)
return lambda x: f1(f2(f3(x)))
def make_decrypter(f1, f2, f3):
f1, f2, f3 = looper(f1), looper(f2), looper(f3)
return lambda x: f3(f2(f1(x)))
Question 7:
def generator():
...
return make_encrypter(f1, f2, f3), make_decrypter(f1, f2, f3)
Question 8:
>>> def troy():... abed = 0... while abed < 3:... britta = lambda: abed... print(abed)... abed += 2... annie = abed... annie += 1... abed = 6 # seasons and a movie... return britta>>> jeff = troy()
troy()的返回值是 britta, britta = lambda: abed [parent: frame1]。 在 frame1 中,abed最后被更新到6, 所以可以理解为 britta() = 6
Question 9
>>> def go(bears):... gob = 3... print(gob)... return lambda ears: bears(gob)>>> gob = 4>>> bears = go(lambda ears: gob)______
>>> bears(gob)______
解:
改写下 go 函数:
def go(bears):
gob =3
print(gob)
def h(ears):
return bears(gob)
return h
根据function go的定义, 如果f是一个single argument函数
go(f) return h, h(ears) will return f(3) (frame1中 gob=3)
Global frame中, gob=4
f1 = lambda ears: gob = lambda ears: 4
f1(3) =4
bears = go(lambda ears: gob) = go(f1)
bears(gob) = bears(4) = go(f1)(4) = f1(3)
f1(3) =4 于是 得出结果为4
Question 10:- def count_cond(condition):
- def f(n):
- count, i=0,1
- while i<=n:
- if condition(n, i):
- count +=1
- i+=1
- return count
- return f
- cond_factors = lambda n, i: n%i ==0
- count_factors = count_cond(cond_factors)
- is_prime= lambda n,i : count_factors(i)==2
- count_primes = count_cond(is_prime)
复制代码 Question 11
这道题与 solution 略有不同:- def cycle(f1, f2, f3):
- def g(n):
- def h(x):
- if n==0:
- return x
- else:
- i =1
- while i<=n:
- if i%3 ==1:
- x = f1(x)
- elif i%3 ==2:
- x = f2(x)
- else:
- x = f3(x)
- i +=1
- return x
- return h
- return g
- add1 = lambda x: x+1
- times2 = lambda x: x*2
- add3 = lambda x: x+3
- my_cycle = cycle(add1, times2, add3)
- identity = my_cycle(0)
- add1_then_double = my_cycle(2)
- do_all_functions = my_cycle(3)
复制代码 |
|