This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
zipper.py
75 lines (58 loc) · 1.8 KB
/
zipper.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
#!/usr/bin/env python
class Empty:
def __init__(this):
return
def __str__(this):
return "Empty()"
def insertAt(this, n, k):
return Cons(k, this)
class Cons:
def __init__(this, first, rest):
this.first = first
this.rest = rest
return
def __str__(this):
return "Cons(" + str(this.first) + ", " + str(this.rest) + ")"
def insertAt(this, n, k):
if n <= 0:
return Cons(k, this)
else:
return Cons(this.first, this.rest.insertAt(n - 1, k))
class Zipper:
def __init__(this, before, after):
this.before = before
this.after = after
def __str__(this):
return "Zipper(" + str(this.before) + ", " + str(this.after) + ")"
def right(this):
return Zipper(Cons(this.after.first, this.before), this.after.rest)
def insert(this, k):
return Zipper(this.before, Cons(k, this.after))
def left(this):
return Zipper(this.before.rest, Cons(this.before.first, this.after))
def list(this):
return this.after
def zipper(l):
return Zipper(Empty(), l)
def main():
l = Cons(0, Cons(1, Cons(2, Cons(3, Cons(4, Empty())))))
print " l = " + str(l)
lp = l.insertAt(3, 2.9)
print " l = " + str(l)
print " lp = " + str(lp)
lpp = lp.insertAt(3, 2.8)
print " lpp = " + str(lpp)
z = zipper(l)
print " z = " + str(z)
spot = z.right().right().right()
print " spot = " + str(spot)
spotp = spot.insert(2.9)
print " spotp = " + str(spotp)
spotpp = spotp.insert(2.8)
print " spotpp = " + str(spotpp)
zp = spotpp.left().left().left()
print " zp = " + str(zp)
lppp = zp.list()
print " lppp = " + str(lppp)
if __name__ == "__main__":
main()