1
1
// \texttt{list.js START} \begin{lstlisting}
2
2
3
3
/**
4
- * makes a pair whose head (first component) is <CODE>x</CODE>
5
- * and whose tail (second component) is <CODE>y</CODE>.
4
+ * **primitive**; makes a pair whose head (first component) is <CODE>x</CODE>
5
+ * and whose tail (second component) is <CODE>y</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE> .
6
6
* @param {value } x - given head
7
7
* @param {value } y - given tail
8
8
* @returns {pair } pair with <CODE>x</CODE> as head and <CODE>y</CODE> as tail.
9
9
*/
10
10
function pair ( x , y ) { }
11
11
12
12
/**
13
- * returns <CODE>true</CODE> if <CODE>x</CODE> is a
14
- * pair and false otherwise.
13
+ * **primitive**; returns <CODE>true</CODE> if <CODE>x</CODE> is a
14
+ * pair and false otherwise; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE> .
15
15
* @param {value } x - given value
16
16
* @returns {boolean } whether <CODE>x</CODE> is a pair
17
17
*/
18
18
function is_pair ( x ) { }
19
19
20
20
/**
21
- * returns head (first component) of given pair <CODE>p</CODE>
21
+ * **primitive**; returns head (first component) of given pair <CODE>p</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
22
22
* @param {pair } p - given pair
23
23
* @returns {value } head of <CODE>p</CODE>
24
24
*/
25
25
function head ( p ) { }
26
26
27
27
/**
28
- * returns tail (second component of given pair <CODE>p</CODE>
28
+ * **primitive**; returns tail (second component of given pair <CODE>p</CODE>; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE>.
29
29
* @param {pair } p - given pair
30
30
* @returns {value } tail of <CODE>p</CODE>
31
31
*/
32
32
function tail ( p ) { }
33
33
34
34
/**
35
- * returns <CODE>true</CODE> if <CODE>x</CODE> is the
36
- * empty list <CODE>null</CODE>, and <CODE>false</CODE> otherwise.
35
+ * **primitive**; returns <CODE>true</CODE> if <CODE>x</CODE> is the
36
+ * empty list <CODE>null</CODE>, and <CODE>false</CODE> otherwise; time: <CODE>Θ(1)</CODE, space: <CODE>Θ(1)</CODE> .
37
37
* @param {value } x - given value
38
38
* @returns {boolean } whether <CODE>x</CODE> is <CODE>null</CODE>
39
39
*/
40
40
function is_null ( x ) { }
41
41
42
42
/**
43
- * Returns <CODE>true</CODE> if
43
+ * **primitive**; returns <CODE>true</CODE> if
44
44
* <CODE>xs</CODE> is a list as defined in the textbook, and
45
45
* <CODE>false</CODE> otherwise. Iterative process;
46
- * time: <CODE>O (n)</CODE>, space: <CODE>O (1)</CODE>, where <CODE>n</CODE>
46
+ * time: <CODE>Θ (n)</CODE>, space: <CODE>Θ (1)</CODE>, where <CODE>n</CODE>
47
47
* is the length of the
48
48
* chain of <CODE>tail</CODE> operations that can be applied to <CODE>xs</CODE>.
49
- * recurses down the list and checks that it ends with the empty list null
49
+ * <CODE>is_list</CODE> recurses down the list and checks that it ends with the empty list null
50
50
* @param {value } xs - given candidate
51
51
* @returns whether {xs} is a list
52
52
*/
53
53
function is_list ( xs ) { }
54
54
55
55
/**
56
- * Given <CODE>n</CODE> values, returns a list of length <CODE>n</CODE>.
57
- * The elements of the list are the given values in the given order.
56
+ * **primitive**; given <CODE>n</CODE> values, returns a list of length <CODE>n</CODE>.
57
+ * The elements of the list are the given values in the given order; time: <CODE>Θ(n)</CODE, space: <CODE>Θ(n)</CODE> .
58
58
* @param {value } value1,value2,...,value_n - given values
59
59
* @returns {list } list containing all values
60
60
*/
61
61
function list ( value1 , value2 , ...values ) { }
62
62
63
63
/**
64
- * visualizes <CODE>x</CODE> in a separate drawing
65
- * area in the Source Academy using a box-and-pointer diagram ; time, space:
66
- * O (n), where n is the total number of data structures such as
67
- * pairs in all the separate structures provided in <CODE>x</CODE> .
64
+ * visualizes the arguments in a separate drawing
65
+ * area in the Source Academy using box-and-pointer diagrams ; time, space:
66
+ * <CODE>Θ (n)</CODE> , where <CODE>n</CODE> is the total number of data structures such as
67
+ * pairs in the arguments .
68
68
* @param {value } value1,value2,...,value_n - given values
69
69
* @returns {value } given <CODE>x</CODE>
70
70
*/
@@ -80,9 +80,10 @@ function list(value1, value2, ...values ) {}
80
80
* need to be the same. If both are <CODE>undefined</CODE> or both are
81
81
* <CODE>null</CODE>, the result is <CODE>true</CODE>. Otherwise they are compared
82
82
* with <CODE>===</CODE> (using the definition of <CODE>===</CODE> in the
83
- * respective Source language in use). Time, space:
84
- * <CODE>O(n)</CODE>, where <CODE>n</CODE> is the number of pairs in
85
- * <CODE>x</CODE>.
83
+ * respective Source language in use).
84
+ * Time, space:
85
+ * <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the total number of data structures such as
86
+ * pairs in <CODE>x</CODE> and <CODE>y</CODE>.
86
87
* @param {value } x - given value
87
88
* @param {value } y - given value
88
89
* @returns {boolean } whether <CODE>x</CODE> is structurally equal to <CODE>y</CODE>
@@ -109,8 +110,8 @@ function equal(xs, ys) {
109
110
/**
110
111
* Returns the length of the list
111
112
* <CODE>xs</CODE>.
112
- * Iterative process; time: <CODE>O (n)</CODE>, space:
113
- * <CODE>O (1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
113
+ * Iterative process; time: <CODE>Θ (n)</CODE>, space:
114
+ * <CODE>Θ (1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
114
115
* @param {list } xs - given list
115
116
* @returns {number } length of <CODE>xs</CODE>
116
117
*/
@@ -124,8 +125,8 @@ function $length(xs, acc) {
124
125
/**
125
126
* Returns a list that results from list
126
127
* <CODE>xs</CODE> by element-wise application of unary function <CODE>f</CODE>.
127
- * Iterative process; time: <CODE>O (n)</CODE>,
128
- * space: <CODE>O (n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
128
+ * Iterative process; time: <CODE>Θ (n)</CODE> (apart from <CODE>f</CODE>) ,
129
+ * space: <CODE>Θ (n)</CODE> (apart from <CODE>f</CODE>) , where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
129
130
* <CODE>f</CODE> is applied element-by-element:
130
131
* <CODE>map(f, list(1, 2))</CODE> results in <CODE>list(f(1), f(2))</CODE>.
131
132
* @param {function } f - unary
@@ -146,7 +147,7 @@ function $map(f, xs, acc) {
146
147
* Makes a list with <CODE>n</CODE>
147
148
* elements by applying the unary function <CODE>f</CODE>
148
149
* to the numbers 0 to <CODE>n - 1</CODE>, assumed to be a nonnegative integer.
149
- * Iterative process; time: <CODE>O (n)</CODE>, space: <CODE>O (n)</CODE>.
150
+ * Iterative process; time: <CODE>Θ (n)</CODE> (apart from <CODE>f</CODE>) , space: <CODE>Θ (n)</CODE> (apart from <CODE>f</CODE>) .
150
151
* @param {function } f - unary function
151
152
* @param {number } n - given nonnegative integer
152
153
* @returns {list } resulting list
@@ -161,8 +162,8 @@ function $build_list(i, fun, already_built) {
161
162
/**
162
163
* Applies unary function <CODE>f</CODE> to every
163
164
* element of the list <CODE>xs</CODE>.
164
- * Iterative process; time: <CODE>O (n)</CODE>, space: <CODE>O (1)</CODE>,
165
- * Where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
165
+ * Iterative process; time: <CODE>Θ (n)</CODE> (apart from <CODE>f</CODE>) , space: <CODE>Θ (1)</CODE> (apart from <CODE>f</CODE>) ,
166
+ * where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
166
167
* <CODE>f</CODE> is applied element-by-element:
167
168
* <CODE>for_each(fun, list(1, 2))</CODE> results in the calls
168
169
* <CODE>fun(1)</CODE> and <CODE>fun(2)</CODE>.
@@ -185,6 +186,9 @@ function for_each(fun, xs) {
185
186
* Returns a string that represents
186
187
* list <CODE>xs</CODE> using the text-based box-and-pointer notation
187
188
* <CODE>[...]</CODE>.
189
+ * Iterative process; time: <CODE>Θ(n)</CODE> where <CODE>n</CODE> is the size of the list, space: <CODE>Θ(m)</CODE> where <CODE>m</CODE> is the length of the string.
190
+ * The process is iterative, but consumes space <CODE>O(m)</CODE>
191
+ * because of the result string.
188
192
* @param {list } xs - given list
189
193
* @returns {string } <CODE>xs</CODE> converted to string
190
194
*/
@@ -205,9 +209,9 @@ function $list_to_string(xs, cont) {
205
209
206
210
/**
207
211
* Returns list <CODE>xs</CODE> in reverse
208
- * order. Iterative process; time: <CODE>O (n)</CODE>,
209
- * space: <CODE>O (n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
210
- * The process is iterative, but consumes space <CODE>O (n)</CODE>
212
+ * order. Iterative process; time: <CODE>Θ (n)</CODE>,
213
+ * space: <CODE>Θ (n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
214
+ * The process is iterative, but consumes space <CODE>Θ (n)</CODE>
211
215
* because of the result list.
212
216
* @param {list } xs - given list
213
217
* @returns {list } <CODE>xs</CODE> in reverse
@@ -224,8 +228,8 @@ function $reverse(original, reversed) {
224
228
/**
225
229
* Returns a list that results from
226
230
* appending the list <CODE>ys</CODE> to the list <CODE>xs</CODE>.
227
- * Iterative process; time: <CODE>O (n)</CODE>, space:
228
- * <CODE>O (n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
231
+ * Iterative process; time: <CODE>Θ (n)</CODE>, space:
232
+ * <CODE>Θ (n)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
229
233
* In the result, null at the end of the first argument list
230
234
* is replaced by the second argument, regardless what the second
231
235
* argument consists of.
@@ -247,8 +251,8 @@ function $append(xs, ys, cont) {
247
251
* whose head is identical to
248
252
* <CODE>v</CODE> (using <CODE>===</CODE>); returns <CODE>null</CODE> if the
249
253
* element does not occur in the list.
250
- * Iterative process; time: <CODE>O (n)</CODE>,
251
- * space: <CODE>O (1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
254
+ * Iterative process; time: <CODE>Θ (n)</CODE>,
255
+ * space: <CODE>Θ (1)</CODE>, where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
252
256
* @param {value } v - given value
253
257
* @param {list } xs - given list
254
258
* @returns {list } postfix sublist that starts with <CODE>v</CODE>
@@ -266,7 +270,7 @@ function member(v, xs) {
266
270
* is identical (<CODE>===</CODE>) to <CODE>v</CODE>.
267
271
* Returns the original
268
272
* list if there is no occurrence. Iterative process;
269
- * time: <CODE>O (n)</CODE>, space: <CODE>O (n)</CODE>, where <CODE>n</CODE>
273
+ * time: <CODE>Θ (n)</CODE>, space: <CODE>Θ (n)</CODE>, where <CODE>n</CODE>
270
274
* is the length of <CODE>xs</CODE>.
271
275
* @param {value } v - given value
272
276
* @param {list } xs - given list
@@ -290,7 +294,7 @@ function $remove(v, xs, acc) {
290
294
* Returns the original
291
295
* list if there is no occurrence.
292
296
* Iterative process;
293
- * time: <CODE>O (n)</CODE>, space: <CODE>O (n)</CODE>, where <CODE>n</CODE>
297
+ * time: <CODE>Θ (n)</CODE>, space: <CODE>Θ (n)</CODE>, where <CODE>n</CODE>
294
298
* is the length of <CODE>xs</CODE>.
295
299
* @param {value } v - given value
296
300
* @param {list } xs - given list
@@ -313,7 +317,7 @@ function $remove_all(v, xs, acc) {
313
317
* <CODE>pred</CODE>
314
318
* returns <CODE>true</CODE>.
315
319
* Iterative process;
316
- * time: <CODE>O (n)</CODE>, space: <CODE>O (n)</CODE>,
320
+ * time: <CODE>Θ (n)</CODE> (apart from <CODE>pred</CODE>) , space: <CODE>Θ (n)</CODE> (apart from <CODE>pred</CODE>) ,
317
321
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
318
322
* @param {function } pred - unary function returning boolean value
319
323
* @param {list } xs - given list
@@ -335,7 +339,7 @@ function $filter(pred, xs, acc) {
335
339
* numbers starting from <CODE>start</CODE> using a step size of 1, until
336
340
* the number exceeds (<CODE>></CODE>) <CODE>end</CODE>.
337
341
* Iterative process;
338
- * time: <CODE>O (n)</CODE>, space: <CODE>O (n)</CODE>,
342
+ * time: <CODE>Θ (n)</CODE>, space: <CODE>Θ (n)</CODE>,
339
343
* where <CODE>n</CODE> is <CODE>end - start</CODE>.
340
344
* @param {number } start - starting number
341
345
* @param {number } end - ending number
@@ -355,7 +359,7 @@ function $enum_list(start, end, acc) {
355
359
* of list <CODE>xs</CODE> at position <CODE>n</CODE>,
356
360
* where the first element has index 0.
357
361
* Iterative process;
358
- * time: <CODE>O (n)</CODE>, space: <CODE>O (1)</CODE>,
362
+ * time: <CODE>Θ (n)</CODE>, space: <CODE>Θ (1)</CODE>,
359
363
* where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
360
364
* @param {list } xs - given list
361
365
* @param {number } n - given position
@@ -380,9 +384,8 @@ function list_ref(xs, n) {
380
384
* list. Thus, <CODE>accumulate(f,zero,list(1,2,3))</CODE> results in
381
385
* <CODE>f(1, f(2, f(3, zero)))</CODE>.
382
386
* Iterative process;
383
- * time: <CODE>O(n)</CODE>, space: <CODE>O(n)</CODE>,
384
- * where <CODE>n</CODE> is the length of <CODE>xs</CODE>
385
- * assuming <CODE>f</CODE> takes constant time.
387
+ * time: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>), space: <CODE>Θ(n)</CODE> (apart from <CODE>f</CODE>),
388
+ * where <CODE>n</CODE> is the length of <CODE>xs</CODE>.
386
389
* @param {function } f - binary function
387
390
* @param {value } initial - initial value
388
391
* @param {list } xs - given list
@@ -399,8 +402,11 @@ function $accumulate(f, initial, xs, cont) {
399
402
400
403
401
404
/**
402
- * Optional second argument.
403
- * Similar to <CODE>display</CODE>, but formats well-formed lists nicely if detected.
405
+ * Optional second argument.
406
+ * Similar to <CODE>display</CODE>, but formats well-formed lists nicely if detected;
407
+ * time, space:
408
+ * <CODE>Θ(n)</CODE>, where <CODE>n</CODE> is the total number of data structures such as
409
+ * pairs in <CODE>x</CODE>.
404
410
* @param {value } xs - list structure to be displayed
405
411
* @param {string } s to be displayed, preceding <CODE>xs</CODE>
406
412
* @returns {value } xs, the first argument value
0 commit comments