-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.py
More file actions
112 lines (77 loc) · 2.43 KB
/
script.py
File metadata and controls
112 lines (77 loc) · 2.43 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
# A token type used for just being Nil
class Nil: # None
def IsNil(self):
return True
class listOf:
def __init__(self, elem, rest):
self.head = elem
self.tail = rest
def IsNil(self):
return False
def Head(self):
return self.head
def Tail(self):
return self.tail
def Cons(elem, rest):
return listOf(elem, rest)
def Sum(a_list):
return foldr(int.__add__, 0, a_list)
def product(a_list):
return foldr(int.__mul__, 1, a_list)
def foldr(opr, initValue, a_list):
# print("foldr")
# if a_list.IsNil():
# print(f"return {initValue}")
# return initValue
# val = foldr(opr, initValue, a_list.Tail())
# head = a_list.Head()
# res = opr(head, val)
# print(f"{head} {opr} {val} == {res}")
# return res
return initValue if a_list.IsNil() else opr(a_list.Head(), foldr(opr, initValue, a_list.Tail()))
def buildList(l):
return Nil() if not l else Cons(l[0], buildList(l[1:]))
def AnyTrue(a_list):
return foldr(bool.__or__, False, a_list)
def AllTrue(a_list):
return foldr(bool.__and__, True, a_list)
def copy(a_list):
return foldr(Cons, Nil(), a_list)
def append(a_list, b_list):
return foldr(Cons, copy(b_list), a_list)
def length(a_list):
return foldr(lambda _,count_so_far: count_so_far + 1, 0, a_list)
def Map(func, a_list):
return foldr(lambda a, b: Cons(func(a), b), Nil(), a_list)
def DoubleAll(a_list):
double = lambda x: x*2
return Map(double, a_list)
myEmptyList = Nil()
print(type(myEmptyList))
l1 = Cons(1, Cons(2, Cons(3, Nil())))
print(type(l1))
# myList = Cons(13, Cons(27, Cons(3, Nil())))
myList = buildList([13,27,3])
print(myList.IsNil()) # False
print(myList.Head()) # 13
print(myList.Tail().Head()) # 27
print(Sum(myList))
print(foldr(int.__add__, 0, myList))
print(product(myList))
print(foldr(int.__mul__, 1, myList))
l = buildList([1,2,3])
print(AnyTrue(buildList([False, False, True])))
print(AnyTrue(buildList([False, False, False])))
print(AnyTrue(buildList([])))
print(AllTrue(buildList([True, True, True])))
print(AllTrue(buildList([False, False, True])))
print(AllTrue(buildList([])))
print("------")
print(product(myList))
list_copy = copy(myList)
print(myList is not list_copy)
print(Sum(append(buildList([1,2]), buildList([3,4]))))
print(length(buildList([1,2,3,4,5,6])))
print("------")
print(Sum(buildList([1,2,3,4,5,6])))
print(Sum(DoubleAll(buildList([1,2,3,4,5,6]))))