1
2
3
def square( x): return x * x
square( 2 + 2)
def square( x )
▶
▶
4
def square(x): return x * x
square(square(3))
5
def square(x): return x * x
square(square(3))
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
func square(x)
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
func square(x)
square(3)
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
func square(x)
square(3)
func square(x)
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
func square(x)
square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
square( square(3) )
func square(x)
square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global] x3
square( square(3) )
func square(x)
square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global] x3
square( square(3) )
func square(x)
square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
square( square(3) )
func square(x) 9 square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
square( square(3) )
func square(x) 9 square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
f2: square [parent=Global]
x9
square( square(3) )
func square(x) 9 square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
f2: square [parent=Global]
x9 Return value 81
81
square( square(3) )
func square(x) 9 square(3)
func square(x) 3
5
def square(x): return x * x
square(square(3))
Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
f2: square [parent=Global]
x9 Return value 81
6
def square(x): return x * x
square(square(3))
1Global frame
square ⚫️ —-> func square(x) [parent=Global]
f1: square [parent=Global]
x3 Return value 9
f2: square [parent=Global]
x9 Return value 81
6
def square(x): return x * x
square(square(3))
1Global frame
square ⚫️ —-> func square(x) [parent=Global]
2
1×3 Return value 9
f2: square [parent=Global]
x9 Return value 81
f1: square [parent=Global]
6
def square(x): return x * x
square(square(3))
1Global frame
square ⚫️ —-> func square(x) [parent=Global]
1: square [parent=Global]
x3 Return value 9
2: square [parent=Global]
x9 Return value 81
2 2
f
1
f
1
6
def square(x): return x * x
square(square(3))
1Global frame
square ⚫️ —-> func square(x) [parent=Global]
1: square [parent=Global]
x3 Return value 9
2: square [parent=Global]
x9 Return value 81
2 2
f
1
f
1
7
def square(square): return square * square
square(4)
8
def square(square): return square * square
square(4) 1Global frame
square ⚫️ —-> func square(square) [parent=Global]
2
1
f1: square [parent=Global]
square 4 Return value 16
8
9
summation(5, lambda x: x**2)
make_adder(3)(1)
10
def apply_twice(f, x): return f(f(x))
def square(x): return x ** 2
apply_twice(square, 3)
11
12
12
12
13
def make_texter(emoji): def texter(text):
return emoji + text + emoji return texter
happy_text = make_texter(“”)
result = happy_text(“lets go to the beach!”)
14
15
15
15
15
func
17
def thingy(x, y): return bobber(y)
def bobber(a): return a + y
result = thingy(“ma”, “jig”)
18
def thingy(x, y): return bobber(y)
def bobber(a): return a + y
result = thingy(“ma”, “jig”)
18
19
20
def happy(text):
return “☻” + text + “☻”
def sad(text):
return “☹” + text + “☹”
def composer(f, g): def composed(x):
return f(g(x)) return composed
msg1 = composer(sad, happy)(“cs61a!”)
msg2 = composer(happy, sad)(“eecs16a!”)
21
def happy(text):
return “☻” + text + “☻”
def sad(text):
return “☹” + text + “☹”
def make_texter(emoji): def texter(text):
return emoji + text + emoji return texter
def composer(f, g): def composed(x):
return f(g(x)) return composed
composer(happy, make_texter(“☃︎”))(‘snow day!’)
22
composer(happy, make_texter(“☃”))(“snow day!”)
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g)
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text)
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text)
make_texter(“☃”)
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text)
make_texter(“☃”)
func make_texter(emoji)
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text)
make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text)
make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”) func composed(x)
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”) func composed(x) “snow day!”
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
composer(happy, make_texter(“☃”))(“snow day!”) func composed(x) “snow day!”
composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
“☻☃snow day!☃☻” composer(happy, make_texter(“☃”))(“snow day!”)
func composed(x) “snow day!” composer(happy, make_texter(“☃”))
func composer(f, g) func happy(text) func texter(text) make_texter(“☃”)
func make_texter(emoji) “☃”
23
24
def print_sums(n): print(n)
def next_sum(k):
return print_sums(n + k)
return next_sum print_sums(1)(3)(5)
25
26
print sums(1)(3)(5)
g1 = print sums(1) g2 = g1(3)
g2(5)
print sums(x)
x
y
x+y x g1
g2
27
28
from operator import add add(2, 3)
def make_adder(n):
return lambda x: n + x
make_adder(2)(3)
add(2, 3)
make_adder(2)(3)
29
def curry2(f): def g(x):
def h(y):
return f(x, y)
return h return g
30
def curry2(f): def g(x):
def h(y):
return f(x, y)
return h return g
make_adder = curry2(add)
make_adder(2)(3)
30
def curry2(f): def g(x):
def h(y):
return f(x, y)
return h return g
make_adder = curry2(add)
make_adder(2)(3)
curry2 = lambda f: lambda x: lambda y: f(x, y)
30
❌❌
31