我们好,日拱一卒,我是梁唐。本文始发于公众号:Coder梁
咱们今日持续来看伯克利CS61A的公开课,这次咱们聊聊hw02,也便是作业2。
这一次的作业关于Python函数式编程,其实哪怕是局限在Python语言傍边,函数式编程也不是一个简单的概念。除了高阶函数之外,还有装饰器、偏函数、MapReduce等等许多内容。
Github
作业说明
但在日常运用傍边,高阶函数的运用往往能够大幅提高代码质量,降低阅览难度,但大多数情况下并不是必须的。我不确定制定课程的教授是如何思考的,以我个人来看,作为初学者而言,了解函数式编程的思想以及它的运用场景,对这个概念熟悉起来,其实要比死记硬背一些所谓高端的用法更有用。
这一次的作业,刚好是一个非常好的用来学习、熟悉函数式编程思想的比如。
废话不多说,咱们来看题。
首要,标题给定了一些代码,界说了一些函数。有惯例函数,也有匿名函数。
def square(x):
return x * x
def identity(x):
return x
triple = lambda x: 3 * x
increment = lambda x: x + 1
add = lambda x, y: x + y
mul = lambda x, y: x * y
在Python傍边,匿名函数以关键字lambda
开头,后边跟函数的传参,冒号之后是函数的回来成果。在这种格式的限定下,使得匿名函数的逻辑往往不能太过复杂,只能用来处理一些简单的核算问题。
咱们简单阅览一下代码,能够看到这几个函数的功用都非常简单。square
是平方,identity
便是回来自身,triple
是乘3,increatment
是自增1,add
是两数相加,mul
是两数相乘。
看完了这段代码之后,开端正式看题。
Q1: Product
课件傍边有一个比如是函数summation(n, term)
,它是一个高阶函数,它回来term(1) + term(2) +...+ term(m)
的成果。
请你模仿summation
函数,完成一个product
函数,来完成term(1) * term(2) *...*term(n)
。
其间课件中的summation
函数代码如下:
def summation(n, term):
"""Sum the first N terms of a sequence.
>>> summation(5, cube)
225
"""
total, k = 0, 1
while k <= n:
total, k = total + term(k), k + 1
return total
核心点在于term
不仅是函数的传参,而且它自身便是一个能够调用的函数。所以只需搞清楚这点,代码非常好写:
def product(n, term):
"""Return the product of the first n terms in a sequence.
n -- a positive integer
term -- a function that takes one argument
>>> product(3, identity) # 1 * 2 * 3
6
>>> product(5, identity) # 1 * 2 * 3 * 4 * 5
120
>>> product(3, square) # 1^2 * 2^2 * 3^2
36
>>> product(5, square) # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
14400
>>> product(3, increment) # (1+1) * (2+1) * (3+1)
24
>>> product(3, triple) # 1*3 * 2*3 * 3*3
162
"""
"*** YOUR CODE HERE ***"
ret = 1
for i in range(1, n+1):
ret *= term(i)
return ret
# The identity function, defined using a lambda expression!
identity = lambda k: k
咱们模仿同样的逻辑,将加好改成乘号即可。
第二个问题是运用product
函数完成阶乘函数factorial
。
def factorial(n):
"""Return n factorial for n >= 0 by calling product.
>>> factorial(4)
24
>>> factorial(6)
720
>>> from construct_check import check
>>> check(HW_SOURCE_FILE, 'factorial', ['Recursion', 'For', 'While'])
True
"""
"*** YOUR CODE HERE ***"
return product(n, identity)
咱们只需要按照逻辑,让n个整数顺次相乘即可。咱们能够传入函数identity
,由于identity(x)=x
。
Q2: Make Adder with a Lambda
完成make_adder
函数,它回来一个lambda
表达式。
本题便是lambda
表达式的根本运用,了解函数式编程之后,也很简单。
def make_adder(n):
"""Return a function that takes an argument K and returns N + K.
>>> add_three = make_adder(3)
>>> add_three(1) + add_three(2)
9
>>> make_adder(1)(2)
3
"""
"*** YOUR CODE HERE ***"
return lambda x: x + n
关于回来的函数lambda x: x+n
而言,这儿的参数n的界说在它的外部。这儿涉及到Python傍边一个很重要的概念叫做——闭包,闭包的原理和使用也是函数式编程中的一个范畴。
我们要是感兴趣能够去研究一下,这儿不做过多的展开了。