CS1101S S6
Extra questions:
-
Implement a
set
data structure using lists. Aset
is an unordered collection of unique items. You will need to implement the following functions:set(xs)
returns a new set containing elements in listxs
.add(s, x)
add itemx
to sets
and return the new set.is_elem(x, x)
checks ifx
is ins
.union(s1, s2)
return a new set that contains all the elements ins1
ands2
.intersect(s1, s2)
return a new set that contains elements in boths1
ands2
.
It does not matter how you back up your data structure, but try to pick the most reasonable!
-
The function
accumulate_n
is similar toaccumulate
except that it takes as its third argument a list of lists, which are all assumed to have the same number of elements. It applies the designated accumulation function to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a list of the results. For example:const s = list(list(1, 2, 3),list(4, 5, 6),list(7, 8, 9),list(10, 11, 12),); // returns list(22, 26, 30).accumulate_n((x, y) => x + y, 0, s);If you want a hint, here is a skeleton:
function accumulate_n(op, init, seqs) {return is_null(head(seqs))? null: pair( ..., ...);} -
Insertion sort is a sorting method that revolves around the
insert
function. What insertion sort does is to basically take one element, and insert it at the correct place in a list.insert(4, xs) ────┐vxs = 1 3 5 9- Implement
insert
. You can use whatever ordering you like. - Implement
insertionsort
using yourinsert
function. - Give the space and time complexity for your implementation.
- Implement