|
| 1 | +\section*{Appendix: List library} |
| 2 | + |
| 3 | +Those list library functions that are not builtins are pre-declared as follows: |
| 4 | + |
| 5 | +\begin{lstlisting} |
| 6 | +// is_list recurses down the list and checks that it ends with the empty list [] |
| 7 | + |
| 8 | +function is_list(xs) { |
| 9 | + return is_empty_list(xs) || (is_pair(xs) && is_list(tail(xs))); |
| 10 | +} |
| 11 | + |
| 12 | +// equal computes the structural equality |
| 13 | +// over its arguments |
| 14 | + |
| 15 | +function equal(item1, item2){ |
| 16 | + return (is_pair(item1) && is_pair(item2)) |
| 17 | + || (is_empty_list(item1) && is_empty_list(item2)) |
| 18 | + || item1 === item2; |
| 19 | +} |
| 20 | + |
| 21 | +// returns the length of a given argument list |
| 22 | +// assumes that the argument is a list |
| 23 | + |
| 24 | +function length(xs) { |
| 25 | + return is_empty_list(xs) |
| 26 | + ? 0 |
| 27 | + : length(tail(xs)); |
| 28 | +} |
| 29 | + |
| 30 | +// map applies first arg f, assumed to be a unary function, |
| 31 | +// to the elements of the second argument, assumed to be a list. |
| 32 | +// f is applied element-by-element: |
| 33 | +// map(f, [1, [2, []]]) results in [f(1), [f(2), []]] |
| 34 | + |
| 35 | +function map(f, xs) { |
| 36 | + return (is_empty_list(xs)) |
| 37 | + ? [] |
| 38 | + : pair(f(head(xs)), map(f, tail(xs))); |
| 39 | +} |
| 40 | + |
| 41 | +// build_list takes a non-negative integer n as first argument, |
| 42 | +// and a function fun as second argument. |
| 43 | +// build_list returns a list of n elements, that results from |
| 44 | +// applying fun to the numbers from 0 to n-1. |
| 45 | + |
| 46 | +function build_list(n, fun){ |
| 47 | + function build(i, fun, already_built) { |
| 48 | + return i < 0 |
| 49 | + ? already_built |
| 50 | + : build(i - 1, fun, pair(fun(i), |
| 51 | + already_built)); |
| 52 | + } |
| 53 | + return build(n - 1, fun, []); |
| 54 | +} |
| 55 | + |
| 56 | +// for_each applies first arg fun, assumed to be a unary function, |
| 57 | +// to the elements of the second argument, assumed to be a list. |
| 58 | +// fun is applied element-by-element: |
| 59 | +// for_each(fun, [1, [2, []]]) results in the calls fun(1) and fun(2). |
| 60 | +// for_each returns true. |
| 61 | + |
| 62 | +function for_each(fun, xs) { |
| 63 | + if (is_empty_list(xs)) { |
| 64 | + return true; |
| 65 | + } else { |
| 66 | + fun(head(xs)); |
| 67 | + return for_each(fun, tail(xs)); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// to_string uses JavaScript's + to turn its argument into a string |
| 72 | + |
| 73 | +function to_string(x) { |
| 74 | + return x + ""; |
| 75 | +} |
| 76 | + |
| 77 | +// list_to_string returns a string that represents the argument list. |
| 78 | +// It applies itself recursively on the elements of the given list. |
| 79 | +// When it encounters a non-list, it applies toString to it. |
| 80 | + |
| 81 | +function list_to_string(xs) { |
| 82 | + return is_empty_list(xs) |
| 83 | + ? "[]" |
| 84 | + : is_pair(xs) |
| 85 | + ? "[" + list_to_string(head(xs)) + ","+ |
| 86 | + list_to_string(tail(xs)) + "]" |
| 87 | + : to_string(xs); |
| 88 | +} |
| 89 | + |
| 90 | +// reverse reverses the argument, assumed to be a list |
| 91 | + |
| 92 | +function reverse(xs) { |
| 93 | + function rev(original, reversed) { |
| 94 | + return is_empty_list(original) |
| 95 | + ? reversed |
| 96 | + : rev(tail(original), |
| 97 | + pair(head(original), reversed)); |
| 98 | + } |
| 99 | + return rev(xs, []); |
| 100 | +} |
| 101 | + |
| 102 | +// append first argument, assumed to be a list, to the second argument. |
| 103 | +// In the result, the [] at the end of the first argument list |
| 104 | +// is replaced by the second argument, regardless what the second |
| 105 | +// argument consists of. |
| 106 | + |
| 107 | +function append(xs, ys) { |
| 108 | + return is_empty_list(xs) |
| 109 | + ? ys |
| 110 | + : pair(head(xs), |
| 111 | + append(tail(xs), ys)); |
| 112 | +} |
| 113 | + |
| 114 | +// member looks for a given first-argument element in the |
| 115 | +// second argument, assumed to be a list. It returns the first |
| 116 | +// postfix sublist that starts with the given element. It returns [] if the |
| 117 | +// element does not occur in the list |
| 118 | + |
| 119 | +function member(v, xs){ |
| 120 | + return is_empty_list(xs) |
| 121 | + ? [] |
| 122 | + : (v === head(xs)) |
| 123 | + ? xs |
| 124 | + : member(v, tail(xs)); |
| 125 | +} |
| 126 | + |
| 127 | +// removes the first occurrence of a given first-argument element |
| 128 | +// in second-argument, assmed to be a list. Returns the original |
| 129 | +// list if there is no occurrence. |
| 130 | + |
| 131 | +function remove(v, xs){ |
| 132 | + return is_empty_list(xs) |
| 133 | + ? [] |
| 134 | + : v === head(xs) |
| 135 | + ? tail(xs) |
| 136 | + : pair(head(xs), |
| 137 | + remove(v, tail(xs))); |
| 138 | +} |
| 139 | + |
| 140 | +// Similar to remove, but removes all instances of v |
| 141 | +// instead of just the first |
| 142 | + |
| 143 | +function remove_all(v, xs) { |
| 144 | + return is_empty_list(xs) |
| 145 | + ? [] |
| 146 | + : v === head(xs) |
| 147 | + ? remove_all(v, tail(xs)) |
| 148 | + : pair(head(xs), |
| 149 | + remove_all(v, tail(xs))); |
| 150 | +} |
| 151 | + |
| 152 | +// filter returns the sublist of elements of the second argument |
| 153 | +// (assumed to be a list), for which the given predicate function |
| 154 | +// returns true. |
| 155 | + |
| 156 | +function filter(pred, xs){ |
| 157 | + return is_empty_list(xs) |
| 158 | + ? xs |
| 159 | + : pred(head(xs)) |
| 160 | + ? pair(head(xs), |
| 161 | + filter(pred, tail(xs))) |
| 162 | + : filter(pred, tail(xs)); |
| 163 | +} |
| 164 | + |
| 165 | +// enumerates numbers starting from start, assumed to be a number, |
| 166 | +// using a step size of 1, until the number exceeds end, assumed |
| 167 | +// to be a number |
| 168 | + |
| 169 | +function enum_list(start, end) { |
| 170 | + return start > end |
| 171 | + ? [] |
| 172 | + : pair(start, |
| 173 | + enum_list(start + 1, end)); |
| 174 | +} |
| 175 | + |
| 176 | +// Returns the item in xs (assumed to be a list) at index n, |
| 177 | +// assumed to be a non-negative integer. |
| 178 | +// Note: the first item is at position 0 |
| 179 | + |
| 180 | +function list_ref(xs, n) { |
| 181 | + return n === 0 |
| 182 | + ? head(xs) |
| 183 | + : list_ref(tail(xs), n - 1); |
| 184 | +} |
| 185 | + |
| 186 | +// accumulate applies an operation op (assumed to be a binary function) |
| 187 | +// to elements of sequence (assumed to be a list) in a right-to-left order. |
| 188 | +// first apply op to the last element and initial, resulting in r1, then to |
| 189 | +// the second-last element and r1, resulting in r2, etc, and finally |
| 190 | +// to the first element and r_n-1, where n is the length of the |
| 191 | +// list. |
| 192 | +// accumulate(op, zero, list(1, 2, 3)) results in |
| 193 | +// op(1, op(2, op(3, zero))) |
| 194 | + |
| 195 | +function accumulate(op, initial, sequence) { |
| 196 | + return is_empty_list(sequence) |
| 197 | + ? initial |
| 198 | + : op(head(sequence), |
| 199 | + accumulate(op, initial, tail(sequence))); |
| 200 | +} |
| 201 | +\end{lstlisting} |
| 202 | + |
| 203 | + |
0 commit comments