-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlength.stringlang
123 lines (113 loc) · 3.02 KB
/
length.stringlang
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
/*
Purely proof of concept implementation of "length" (using a just as instrumental function "succ").
With these functions arithmetic operations are easily implemented (see functions.stringlang for examples,
note however that the "length" function there is built-in, i.e. provided by Go)
*/
fun last(s) {
curr = "";
i = "0";
while ((curr = s[i]) != "") {
last = curr;
i = succ(i)
};
last
}
fun init(s) {
init = "";
i = "0";
curr = "";
next = s[i];
while (next != "") {
init = init + curr;
curr = next;
i = succ(i);
next = s[i]
};
init
}
fun tail(s) {
tail = "";
i = "1";
while (s[i] != "") {
tail = tail + s[i];
i = succ(i)
};
tail
}
fun reverse(s) {
if (s == "") {
""
} else {
last(s) + reverse(init(s))
}
}
fun succLarge(n) {
nRev = reverse(n); /* Convert big-endian to little-endian for easier working with */
res = "";
if (nRev["0"] == "9") {
/* Carry */
tailOrZero = if (tail(nRev) == "") { "0" } else { tail(nRev) };
/* Need to temporarily convert to big-endian such that succ works */
res = "0" + reverse(succ(reverse(tailOrZero)))
} else {
res = succ(nRev["0"]) + tail(nRev)
};
reverse(res) /* Back to big-endian from little-endian */
}
/*
succ(n) only works due to the fact that a number's base-10 representation can be walked through using
only log n bits, allowing computation of the successor of a large number n to only actually need the numbers from
0 to log n. Because this process is recursive, if we provide a base case we can bootstrap ourselves from that
to all natural numbers.
*/
fun succ(n) {
/* Bootstrap all the base cases */
if (n == "0") {
"1"
} else if (n == "1") {
"2"
} else if (n == "2") {
"3"
} else if (n == "3") {
"4"
} else if (n == "4") {
"5"
} else if (n == "5") {
"6"
} else if (n == "6") {
"7"
} else if (n == "7") {
"8"
} else if (n == "8") {
"9"
} else {
/* Recursively handle larger values */
succLarge(n)
}
}
fun length(s) {
i = "0";
while (s[i] != "") {
i = succ(i)
};
i
}
"last of bungaloo = " + last("bungaloo") + "\n" +
"init of bungaloo = " + init("bungaloo") + "\n" +
"tail of bungaloo = " + tail("bungaloo") + "\n" +
"reverse of bungaloo = " + reverse("bungaloo") + "\n" +
"length of bungaloo = " + length("bungaloo") + "\n" +
"succ of 9 = " + succ("9") + "\n" +
"succ of 41 = " + succ("41") + "\n" +
"succ of 9199999999999999999999999999999999999999999999999999 = " + succ("9199999999999999999999999999999999999999999999999999")
/*
Returns:
last of bungaloo = o
init of bungaloo = bungalo
tail of bungaloo = ungaloo
reverse of bungaloo = oolagnub
length of bungaloo = 8
succ of 9 = 10
succ of 41 = 42
succ of 9199999999999999999999999999999999999999999999999999 = 9200000000000000000000000000000000000000000000000000
*/