1
2
3
def curry2(f): def g(x):
def h(y):
return f(x, y)
return h return g
from operator import add make_adder = curry2(add)
make_adder(2)(3)
curry2 = lambda f: lambda x: lambda y: f(x, y)
4
def transform_numbers(num1, num2, num3, transform):
return transform(num1), transform(num2), transform(num3)
transform_numbers(3, 4, 5, curry2(add)(60))
5
def transform_numbers(num1, num2, num3, transform):
return transform(num1), transform(num2), transform(num3)
transform_numbers(3, 4, 5, curry2(add)(60))
transform_numbers(3, 4, 5, lambda x: add(60, x))
5
def html_tag(tag_name, text):
return “<" + tag_name + ">” + text + “" + tag_name + ">”
p_tag = curry2(html_tag)(“p”)
p_tag(“hello hello”)
6
def html_tag(tag_name, text):
return “<" + tag_name + ">” + text + “" + tag_name + ">”
p_tag = curry2(html_tag)(“p”)
p_tag(“hello hello”)
import functools
p_tag = functools.partial(html_tag, “p”)
p_tag(“hello hello”)
6
7
8
def trace1(f):
“””Return a function that takes a single argument, x, prints it, computes and prints F(x), and returns the computed value.
>>> square = lambda x: x * x
>>> trace1(square)(3)
-> 3
<- 9
9
"""
9
def trace1(f):
"""Return a function that takes a single argument, x, prints it, computes and prints F(x), and returns the computed value.
>>> square = lambda x: x * x
>>> trace1(square)(3)
-> 3
<- 9
9
"""
def traced(x):
print("->“, x) r = f(x) print(“<-", r) return r
return traced
9
@trace1
def square(x):
return x * x
def square(x): return x * x
square = trace1(square)
10
@ATTR
def aFunc(...):
...
def aFunc(...): ...
aFunc = ATTR(aFunc)
ATTR
11
12
5
print(5)
print(print(5))
13
55 print(5) print(print(5))
13
555 print(5)
print(print(5))
13
555 print(5)
print(print(5))
>> 5 5
13
555 print(5) None print(print(5))
>> 5 5
13
555 print(5) None 5 print(print(5))
>> 5 5
13
555
print(5)
print(print(5))
>> 5
5
>>> print(5)
5
None 5
13
555 print(5) None 5 print(print(5)) None
>> 5
5
>>> print(5)
5
13
555 print(5) None 5 print(print(5)) None 5
None
>> 5
5
>>> print(5)
5
13
555 print(5) None 5 print(print(5)) None 5
None
>> 5
5
>>> print(5)
5
>>> print(print(5))
5
None
13
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)()
delay(delay)()(6)()
print(delay(print)()(4))
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)() 6 delay(delay)()(6)()
print(delay(print)()(4))
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)() 6 delay(delay)()(6)()
print(delay(print)()(4))
delayed 6
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)()
delay(delay)()(6)() 6
print(delay(print)()(4))
6 delayed 6
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)() 6 delay(delay)()(6)() 6
print(delay(print)()(4))
delayed 6
delayed
delayed
6
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)() 6 delay(delay)()(6)() 6
print(delay(print)()(4)) None
delayed 6
delayed
delayed
6
14
def delay(arg): print(‘delayed’) def g():
return arg return g
delay(6)() 6 delay(delay)()(6)() 6
print(delay(print)()(4)) None
delayed 6
delayed
delayed
6
delayed
4
None
14
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’)
add(pirate(3)(square)(4), 1)
pirate(pirate(pirate))(5)(7)
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’)
add(pirate(3)(square)(4), 1)
pirate(pirate(pirate))(5)(7)
‘scurvy’
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’)
add(pirate(3)(square)(4), 1)
pirate(pirate(pirate))(5)(7)
‘scurvy’
matey
‘scurvy’
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’) add(pirate(3)(square)(4), 1) 17 pirate(pirate(pirate))(5)(7)
‘scurvy’
matey
‘scurvy’
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’) ‘scurvy’ add(pirate(3)(square)(4), 1) 17 pirate(pirate(pirate))(5)(7)
matey
‘scurvy’
matey 17
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’) ‘scurvy’ add(pirate(3)(square)(4), 1) 17 pirate(pirate(pirate))(5)(7) Error
matey
‘scurvy’
matey 17
15
def pirate(arggg): print(‘matey’)
def plunder(arggg): return arggg
return plunder
pirate(‘treasure’)(‘scurvy’)
add(pirate(3)(square)(4), 1)
pirate(pirate(pirate))(5)(7)
‘scurvy’
17
Error
matey
‘scurvy’
matey 17
matey
matey
Error
15
def horse(mask): horse = mask
def mask(horse): return horse
return horse(mask)
mask = lambda horse: horse(2)
horse(mask)
Global frame
f1:
horse
mask
Return value
f2:
Return value
f3:
Return value
16
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while ___________________________:
last = n % 10
n = n // 10
if __________________________:
kept = __________________
digits = ________________ return ___________________________
17
def remove(n, digit):
“””Return digits of non-negative N that are not DIGIT, for some non-negative DIGIT less than 10. >>> remove(231, 3)
21
>>> remove(243132, 2)
4313
“””
kept = 0
digits = 0
while n > 0:
last = n % 10
n = n // 10
if last != digit:
kept = kept + (last * 10 ** digits)
digits = digits + 1 return kept
18