List Get Item Recursive Property

the element at index i of stack [ x, y ] = the element at index (i - 1) of stack y

This property tells us how we can retrieve an element at any given index. The plan is to use index as a counter as we pop elements from the list. When the counter reaches 0, then we have arrived at the elemnt we want, and we can use the base property to get the element at index 0.

For example, consider the following:

the element at index 1 of stack [ 10, [ 31, [ 8, [ ] ] ] ] = the element at index (1 - 1) of stack [ 31, [ 8, [ ] ] ]

Since 1 - 1 = 0, the above simplifies to:

the element at index 1 of stack [ 10, [ 31, [ 8, [ ] ] ] ] = the element at index 0 of stack [ 31, [ 8, [ ] ] ]

We just popped 10 from the list on the left and decremented the index by 1.

Now, we know that the element at index 0 of list [31, [8,[]]] is simply 31. Then, using the Transitive Property, we conclude that:

the element at index 1 of stack [ 10, [ 31, [ 8, [ ] ] ] ] = 31

Examples

the element at index 1 of stack [ True, [ False, [ ] ] ] = the element at index (1 - 1) of stack [ False, [ ] ]

the element at index 3 of stack [ "Apple", [ "Banana", [ "Cherry", [ "Date", [ ] ] ] ] ] = the element at index (3 - 1) of stack [ "Banana", [ "Cherry", [ "Date", [ ] ] ] ]

the element at index 5 of stack [ 10, [ 20, [ 40, [ 80, [ 160, [ 320, [ ] ] ] ] ] ] ] = the element at index (5 - 1) of stack [ 20, [ 40, [ 80, [ 160, [ 320, [ ] ] ] ] ] ]

Retrieving an item recursively feels inefficient since it forces us to pop only 1 element at a time, but this allows us to define one property for lists of all sizes.


Comments

Please log in to add comments