Lecture 1 Intro
Lecture 2 Function
- 函数签名Function signature
函数体Function body
Frame
scope-作用域
frame-栈帧
- 表示程序运行时函数调用栈
Lecture 3 Control & Iteration
lamda 匿名函数
另一种条件句形式
函数print的返回值是None
Lecture 4 Higher-Order Functions
Higher-Order Functions 高阶函数
currying 柯里化 把多参数函数化为单一参数高阶函数
Lecture 5 Environments
Decorator 装饰器
Midterm
This expression/Evaluates to/Interactive Output
def delay(arg) print('delayed') def g(): return arg return g delay(delay)()(6)() """">>> delayed delayed 6 """
Lecture 6 Recurion
Verifying Recurion Function
- Verify the base case
- Treat fact as a functional abstration
- Assume that fact(n-1) is correct
- Verify that fact(n) is correct, assuming that fact(n-1) is correct
Mutual Recurion 相互递归
def luhn_sum(n):
if n < 10:
return n
else:
all_but_last, last = split(n)
return luhn_sum_double(all_but_last) + last
def luhn_sum_double(n):
all_but_last, last = split(n)
luhn_digit = sum_digits(2 * last)
if n < 10:
return luhn_digit
else:
return luhn_sum(all_but_last) + luhn_digit
Lecture 7 Tree Recursion
Tree-shaped processes arise whenever executing the body of a recursive function makes more than one call to that function.
Lecture 8 Sequences & Data Abstraction
- Lists 列表
- for statement - sequence unpacking
- range 注意序列值域,左包括,右不包括
- List comprehension 列表推导式
语法规范:
out_list = [out_express for out_express in input_list if out_express_condition]
其中的 if
条件判断根据需要可有可无。
- Abstraction barrier 以有理数的抽象为例
违反抽象界限的例子:
- Data Abstraction 只关心通过数据“行为”进行的表示,而不关心具体实现——即通过数据使用的行为可定义数据的表示。
例:使用函数而非列表构造的有理数Data Implementation
Dictionary 字典 a congregation of key-value pairs 键值对的集合
Lists的复杂操作:
Lecture 9 Functional Decomposition & Debugging
关于python编程应对错误的几个特性
reduce try
Lecture 10 Tree
- Slicing
切片,会创建新的list