Set List Element at Index Base Property
set index 0 of remaining stack [ x, rest ] to value with visited : visited = result of dumping [ value, visited ] to rest
This property says that once we reach index 0, we can perform the update by doing the following:
- Pop the first element of the "remaining" list (we are trying to replace this element so we don't need it).
- Prepend the new value to the "visited" list
- Prepend the reverse of the "visited" list to the "remaining" list.
This is pretty confusing, so let's look at an example:
set index 0 of remaining stack [ 10, [ 15, [ ] ] ] to 20 with visited : [ 5, [ ] ] = result of dumping [ 20, [ 5, [ ] ] ] to [ 15, [ ] ]
- First we remove 10 from [10, [15, []]] so that the "remaining" list is just [15, []].
- We prepend 20 to the visited list, which is [5, []]. The result is [20, [5, []]].
- We reverse [20, [5, []]], resulting in [5, [20, []]].
- Finally, we add this to the beginning of the new "remaining" list [15, []]. The end result is [5, [20, [15, []]]
We need to reverse the "visited" list because we've been adding elements to it in reverse order while decrementing the index.
Examples
set index 0 of remaining stack [ 4, [ 6, [ 8, [ ] ] ] ] to 10 with visited : [ 2, [ ] ] = result of dumping [ 10, [ 2, [ ] ] ] to [ 6, [ 8, [ ] ] ]
set index 0 of remaining stack [ "Cherry", [ ] ] to Cashew with visited : [ "Banana", [ "Apple", [ ] ] ] = result of dumping [ Cashew, [ "Banana", [ "Apple", [ ] ] ] ] to [ ]
set index 0 of remaining stack [ True, [ False, [ ] ] ] to False with visited : [ ] = result of dumping [ False, [ ] ] to [ False, [ ] ]
Comments
Please log in to add comments