Lists, as you’ve seen them until now, permit only sequential access. You can’t get to the third element until you’ve been through the first two. This means that when you recur, you do so on a list that is only one element smaller.
For some computations, this is particularly problematic if you’re working on a multi-processor, such as a machine with many cores. Even with just two processors, the amount of work the two would do will be very imbalanced because one is processing a single element while the other is processing the entire rest of the list. It would be much better if we could split the list roughly in half, and thus (in some cases) reduce the overall processing time by half. That requires a different kind of list data structure, which is what we’re going to explore.
| join-list(list1 :: JoinList, list2 :: JoinList, length :: Number)
Due to the nature of the assignment, you are limited to using only the following functions from the support code:
JoinList.join(other :: JoinList) -> JoinList
Given another join list, it joins them to produce a new join list. If one or both of the join lists are empty-join-list, join will simply return the other list. Otherwise it will return a join list containing the first list prepended onto the second.
split(jlist :: JoinList(is-join-list), proc :: (JoinList, JoinList -> Any)) -> Any
This consumes a join list with two or more elements as the first argument. As the second argument, it takes a handler which consumes two JoinLists. First, split divides its first argument into a prefix and suffix which are non-empty JoinLists that will result in the original list if joined together. Then split invokes its second argument on this prefix and suffix and returns what that invocation returns. There is no guarantee that split will divide the list at the most recent join.
JoinList supports ==, which checks whether the two JoinLists have the same elements in the same order.
For purposes of testing you may use the functions
join-list-to-list(jlist :: JoinList) -> List and
list-to-join-list(lst :: List) -> JoinList
because otherwise your tests will be inconvenient to write. However, you may not use these—
or any functions other than those listed above— to implement the required functionality.
Use the fields of the join-list variant of JoinList. You should only access the list contents via split. This is meant to emulate what would happen in a truly parallel implementation: the list may be “rebalanced” by split, which would be lost if you access the fields directly.
It may be tempting to write j-first and j-rest, then implement the other functions with j-first and j-rest as you would for normal Pyret lists. Don’t do this. The functions you write for should take advantage of the unique structure of join lists. It may be necessary to use j-first and j-rest in some situations, but you should use split instead wherever possible.
While we are not grading on the efficiency of your code per-se, if your implementation is extremely inefficient, that is probably a sign that you are not using the structure of join lists properly, and you might want to rethink your implementation.
Implement the following functions. Some of them require non-empty join lists. This is specified in the type signature as JoinList%(is-non-empty-jl)).
j-first(jl :: JoinList%(is-non-empty-jl)) -> Any
Returns the first element of a non-empty list.
j-rest(jl :: JoinList%(is-non-empty-jl)) -> JoinList
Returns a join list containing all elements but the first of a non-empty list.
j-length(jl :: JoinList) -> Number
Returns the length of a join list.
j-nth(jl :: JoinList%(is-non-empty-jl), n :: Number) -> Any
Returns the nth element (using a 0-based index) of a list containing at least n elements. For example, a second argument of 0 returns the first element of the join list.
j-max(jl :: JoinList%(is-non-empty-jl), cmp :: (Any, Any -> Boolean)) -> Any
Returns the “maximum” value in a non-empty list. The second argument is a comparator that defines “maximum” for the type in the list: if the comparator returns true, the first argument to the comparator is “greater” than its second argument.
j-map(map-fun :: (Any -> Any), jl :: JoinList) -> JoinList
Implements map for join lists.
j-filter(filter-fun :: (Any -> Boolean), jl :: JoinList) -> JoinList
Implements filter for join lists.
j-reduce(reduce-func :: (Any, Any -> Any), jl :: JoinList%(is-non-empty-jl)) -> Any
Distributes an operator across a non-empty list. That is, given the elements e1, e2, ..., en in order, and operator op, computes the equivalent of e1 op e2 op ... op en.
j-sort(cmp-fun :: (Any, Any -> Boolean), jl :: JoinList) -> JoinList
Sorts a list in the order given by the second argument, the comparator. If the comparator returns true, then the first argument to the comparator should come before the second argument in the list produced.
What should the value of
j-reduce(lam(x,y): x - y end, list-to-join-list([list: 1, 2, 3]))
be? Hint: There is more than one possible answer. List all.
Unfortunately, having more than one answer violates the expectation that j-reduce is a function. The problem is not with j-reduce but with the use of - (minus) as an argument. What property should the first argument to j-reduce demonstrate? Can you argue this from the description of j-reduce above?
This assignment requires you to implement many different functions. In your sweep we suggest you test some of the higher-level functionality such as j-map, j-filter, j-sort, and j-reduce.
As with previous assignments, you will submit an initial test sweep. This is due 11:59 PM, Sunday, November 16th.
If you decide to opt-in for the test review, please complete your reviews no later than 11:59 PM, Monday, November 17th.
Unlike previous assignments, you will not be submitting a separate test file. This does not mean that you will not be graded on your tests! Rather, you should write tests inside your implemetation itself, as you did earlier in the year.
To submit, return to the Captain Teach assignments page:
and click “Next Step” again. This time, upload a zip file containing both your implementation, named join-lists.arr, and your analysis.
Note that you will submit twice from Captain Teach—