-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture_5
More file actions
60 lines (30 loc) · 730 Bytes
/
Lecture_5
File metadata and controls
60 lines (30 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
This lecture we are still going to study higher-order functions. The core of almost all programming languages.
We shall begin with an example:
def apply_twice(f, x):
return f(f(x))
def square(x):
return x * x
>>> apply_twice(square, 2)
16
Now let's examining a new kind of function:
{
def repeat(f, x):
while f(x) != x
x = f(x)
return x
def g(y):
return (y + 5) // 3
z = repeat(g, 7)
}
One shall examine and understand the above function completely.
Another example of nested functions:
{
def square(x):
return x * x
def triple(x):
return 3 * x
def compose(f, g):
def h(x):
return f(g(x))
>>> squiple = compose(square, triple)
>>> squiple(5) = 225