04-Higher-Order_Functions_1pps
04-Higher-Order_Functions_1pps
Wednesday, January 28
Announcements
§ Check your submission on ok.cs61a.org and submit again if it's not right
§ If you receive 0/3, talk to your TA (or me) about how to approach the course
• Extra lectures: Earn 1 unit (pass/no pass) by learning about optional additional topics
§ First extra lecture: Thursday 1/29 5-6:30pm in 2050 VLSB (Come there to learn more)
2
Iteration Example
The Fibonacci Sequence
0,
1,
1,
2,
3,
5,
8,
13
,
21
,
34
,
55
,
89
,
14
4,
23
3,
def fib(n): 37
7,
"""Compute the nth Fibonacci number, for N >= 1.""" 61
0,
pred, curr = 0, 1 # Zeroth and first Fibonacci numbers 98
7
k = 1 # curr is the kth Fibonacci number
while k < n:
pred, curr = curr, pred + curr
k = k + 1
return curr The next Fibonacci number is the sum of
the current one and its predecessor
4
Discussion Question 1
2 · (n + 1)
a b
I'm still here
n2 + 1
n · (n + 1)
5
Designing Functions
Characteristics of Functions
A function's domain is the set of all inputs it might possibly take as arguments.
!
x is a real number n is an integer greater than or equal to 1
!
!
returns a non-negative returns a Fibonacci number
real number
!
A pure function's behavior is the relationship it creates between input and output.
7
A Guide to Designing Function
!
not
!
Don’t repeat yourself (DRY). Implement a process just once, but execute it many times.
8
Generalization
Generalizing Patterns with Arguments
Shape:
r r
r
p
3 3 2
Area: 1·r 2 ⇡ · r2 ·r
2
Finding common structure allows for shared implementation
(Demo)
10
Higher-Order Functions
Generalizing Over Computational Processes
The common structure among functions may be a computational process, rather than a number.
5
X
k =1+2+3+4+5 = 15
k=1
5
X
k 3 = 13 + 23 + 33 + 43 + 53 = 225
k=1
5
X 8 8 8 8 8 8
= + + + + = 3.04
(4k 3) · (4k 1) 3 35 99 195 323
k=1
(Demo)
12
hof.py
return total
Summation Example
def identity(k):
return k
Function of a single argument
def cube(k): (not called "term")
return pow(k, 3)
A formal parameter that will
def summation(n, term): be bound to a function
"""Sum the first n terms of a sequence.
>>> summation(5, cube)
225
The cube function is passed
""" as an argument value
total, k = 0, 1
while k <= n:
total, k = total + term(k), k + 1
return total
The function bound to term
0 + 1 + 8 + 27 + 64 + 125
def pi_term(k): gets called here
return 8 / (k * 4 − 3) / (k * 4 − 1)
13
def make_adder(n):
"""Return a function that takes one argument k and re
Functions as Return Values
(Demo)
def summation(n, term):
"""Sum the first n terms of a sequence.
>>> summation(5, cube)
225
"""
total, k = 0, 1
while k <= n:
Locally Defined Functions
total, k = total + term(k), k + 1
return total
Functions defined within other function bodies are bound to names in a local frame
def pi_term(k):
return 8 / (k * 4 − 3) / (k * 4 − 1)
A function that
# Local afunction
returns function definitions; returning functions
def make_adder(n):
"""Return a function that takes one argument k and returns k + n.
Operator Operand
3
make_adder(1) ( 2 )
func adder(k) 2
make_adder(1)
func adder(k)
16
Environments for Higher-Order Functions
Names can be Bound to Functional Arguments
18
Interactive Diagram
Discussion Question
If you think
there's an error
19
Interactive Diagram