Set List Element at Index Recurse Property

set index idx of remaining stack [ x, rest ] to value with visited : y = set index (idx - 1) of remaining stack rest to value with visited : [ x, y ]

This property says that the setting an element of a list at some index is equal to popping the first element from the list, pushing it into the beginning of the "visited" list, and decreasing the index by 1. By the time index reaches 0, the first element in the list is the element that we want to set to a new value.

For example, consider the following:

set index 1 of remaining stack [ 5, [ 10, [ 15, [ ] ] ] ] to 20 with visited : [ ] = set index (1 - 1) of remaining stack [ 10, [ 15, [ ] ] ] to 20 with visited : [ 5, [ ] ]

After we take away 5 and decrease the index, the list is now [10, [15, []]] and the index is 0. Since the first element is now 10, we know that we should update 10 to 20.

Notice that when we "pop" from the front and "push" to the front many times, we are effectively copying elements in reverse.

For example, suppose that list A is initially [1, [2, [3, []]]] and list B is initially empty. Let's keep popping from list A and pushing to list B until list A is empty:
TimeList AList B
0[1, [2, [3, []]]][ ]
1[2, [3, []]][1, [] ]
2[3, []][2, [1, [] ]]
3 [][3, [2, [1, [] ]]]

We can see that the final list B is the reverse of the initial list A.

Examples

set index 1 of remaining stack [ 2, [ 4, [ 8, [ ] ] ] ] to 10 with visited : [ ] = set index (1 - 1) of remaining stack [ 4, [ 8, [ ] ] ] to 10 with visited : [ 2, [ ] ]

set index 2 of remaining stack [ "Apple", [ "Orange", [ "Tomato", [ ] ] ] ] to Melon with visited : [ ] = set index (2 - 1) of remaining stack [ "Orange", [ "Tomato", [ ] ] ] to Melon with visited : [ "Apple", [ ] ]

set index 1 of remaining stack [ True, [ False, [ ] ] ] to True with visited : [ ] = set index (1 - 1) of remaining stack [ False, [ ] ] to True with visited : [ True, [ ] ]


Comments

Please log in to add comments