forked from zacker330/psydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.py
155 lines (121 loc) · 3.05 KB
/
lists.py
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
####################################################################
## lists
####################################################################
class PairIterator:
def __init__(self, p):
self.p = p
def next(self):
if self.p == nil:
raise StopIteration
ret = self.p.fst
self.p = self.p.snd
return ret
class Nil:
def __repr__(self):
return "()"
def __iter__(self):
return PairIterator(self)
nil = Nil()
class Pair:
def __init__(self, fst, snd):
self.fst = fst
self.snd = snd
def __repr__(self):
if (self.snd == nil):
return "(" + repr(self.fst) + ")"
elif (isinstance(self.snd, Pair)):
s = repr(self.snd)
return "(" + repr(self.fst) + " " + s[1:-1] + ")"
else:
return "(" + repr(self.fst) + " . " + repr(self.snd) + ")"
def __iter__(self):
return PairIterator(self)
def __eq__(self, other):
if not isinstance(other, Pair):
return False
else:
return self.fst == other.fst and self.snd == other.snd
def loner(u):
return Pair(u, nil)
def foldl(f, x, ls):
ret = x
for y in ls:
ret = f(ret, y)
return ret
def length(ls):
ret = 0
for x in ls:
ret = ret + 1
return ret
def remove(x, ls):
ret = nil
for y in ls:
if x <> y:
ret = Pair(y, ret)
return reverse(ret)
def assoc(u, v):
return Pair(Pair(u, v), nil)
def slist(pylist):
ret = nil
for i in xrange(len(pylist)):
ret = Pair(pylist[len(pylist)-i-1], ret)
return ret
def pylist(ls):
ret = []
for x in ls:
ret.append(x)
return ret
def maplist(f, ls):
ret = nil
for x in ls:
ret = Pair(f(x), ret)
return reverse(ret)
def reverse(ls):
ret = nil
for x in ls:
ret = Pair(x, ret)
return ret
def filterlist(f, ls):
ret = nil
for x in ls:
if f(x):
ret = Pair(x, ret)
return reverse(ret)
# def append(*lists):
# ret = nil
# i = 0
# while i < len(lists):
# ls = lists[i]
# while ls <> nil:
# ret = Pair(ls.fst, ret)
# ls = ls.snd
# i += 1
# return ret
def append(*lists):
def append1(ls1, ls2):
ret = ls2
for x in ls1:
ret = Pair(x, ret)
return ret
return foldl(append1, nil, slist(lists))
def assq(x, s):
for p in s:
if x == p.fst:
return p
return None
def ziplist(ls1, ls2):
ret = nil
while ls1 <> nil and ls2 <> nil:
ret = Pair(Pair(ls1.fst, ls2.fst), ret)
ls1 = ls1.snd
ls2 = ls2.snd
return reverse(ret)
# building association lists
def ext(x, v, s):
return Pair(Pair(x, v), s)
def lookup(x, s):
p = assq(x, s)
if p <> None:
return p.snd
else:
return None