-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw2
More file actions
159 lines (122 loc) · 3.07 KB
/
hw2
File metadata and controls
159 lines (122 loc) · 3.07 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def square(x):
"""Return x squared."""
return x * x
# Q1.
def product(n, term):
"""Return the product of the first n terms in a sequence.
term -- a function that takes one argument
>>> product(4, square)
576
"""
a = 1
k = 1
while k <= n:
a = a * term(k)
k = k + 1
return a
def factorial(n):
"""Return n factorial for n >= 0 by calling product.
>>> factorial(4)
24
"""
k = 1
a = 1
while k <= n:
a = a * k
k = k + 1
return a
# Q2.
def accumulate(combiner, start, n, term):
"""Return the result of combining the first n terms in a sequence."""
loop = n
result = start
while loop > 0:
result = combiner(term(loop), result)
loop -= 1
return result
def summation_using_accumulate(n, term):
"""An implementation of summation using accumulate.
>>> summation_using_accumulate(4, square)
30
"""
def combine(current_term, results):
return current_term + results
return accumulate(combine, 0, n, term)
def product_using_accumulate(n, term):
"""An implementation of product using accumulate.
>>> product_using_accumulate(4, square)
576
"""
def combine(current_term, results):
return current_term * results
return accumulate(combine, 1, n, term)
# Q3.
def double(f):
"""Return a function that applies f twice.
f -- a function that takes one argument
>>> double(square)(2)
16
"""
def double2(x):
return f(f(x))
return double2
# Q4.
def repeated(f, n):
"""Return the function that computes the nth application of f.
f -- a function that takes one argument
n -- a positive integer
>>> repeated(square, 2)(5)
625
>>> repeated(square, 4)(5)
152587890625
"""
def f2(x):
loop = n
result = x
while loop > 0:
result = f(result)
loop -= 1
return result
def compose1(f, g):
"""Return a function h, such that h(x) = f(g(x))."""
def h(x):
return f(g(x))
return h
# Q5. (Optional, reserved for experts)
def zero(f):
return lambda x: x
def successor(n):
return lambda f: lambda x: f(n(f)(x))
def one(f):
"""Church numeral 1."""
"*** YOUR CODE HERE ***"
def two(f):
"""Church numeral 2."""
"*** YOUR CODE HERE ***"
def church_to_int(n):
"""Convert the Church numeral n to a Python integer.
>>> church_to_int(zero)
0
>>> church_to_int(one)
1
>>> church_to_int(two)
2
"""
"*** YOUR CODE HERE ***"
def add_church(m, n):
"""Return the Church numeral for m + n, for Church numerals m and n.
>>> three = successor(two)
>>> church_to_int(add_church(two, three))
5
"""
"*** YOUR CODE HERE ***"
def mul_church(m, n):
"""Return the Church numeral for m * n, for Church numerals m and n.
>>> three = successor(two)
>>> four = successor(three)
>>> church_to_int(mul_church(two, three))
6
>>> church_to_int(mul_church(three, four))
12
"""
"*** YOUR CODE HERE ***"