-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStandardLibrary.verse
62 lines (53 loc) · 1.3 KB
/
StandardLibrary.verse
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
fac(x:int):int {
if(x=0) { 1 + 0 } else { fac(x-1) * x}
}
headInt(x:collection):int {
x[0]
}
headString(x:collection):string{
x[0]
}
append(x:collection, y:collection):collection {
for{a:int; b:int; x[a] | y[b] }
}
tail(x:collection):collection {
for{i:int; i>0; x[i]}
}
cons(x:int, xs:collection):collection{
for{i:int; x | xs[i]}
}
snoc(xs:collection, x:int):collection{
for{i:int; xs[i] | x}
}
sort(x:collection):collection{
if(head := headInt(x)) {
xs := tail(x)
smaller := sort(for{i:int; xs[i] <= head; xs[i]})
larger := sort(for{i:int; xs[i] > head; xs[i]})
append(append(smaller, for{head}), larger)
}
else{
array()
}
}
diff(x:collection, y:collection):collection {
if(h1 := headString(x)) {
if(h2 := headString(y)){
if(h1 = h2) {
next := diff(tail(x), tail(y))
append(for{1}, next)
}
else {
next := diff(tail(x), tail(y))
append(for{0}, next)
}
}
}
}
hanoi(n:int, first:string, second:string, third:string):void {
if(n > 0) {
hanoi(n - 1, first, third, second)
Print("Moved disk from " + first + " to " + third)
hanoi(n - 1, second, third, first)
}
}