Get List Length Property

This property describes how LW Python computes the length of a list. LW Python first looks in the "Value" stack to find the location of the list in the "Object Store," then counts the number of elements in the list. More formally,

if the following are true:
  • Value Stack at time t = [ [ Python reference idx, [ ] ], [ next_level, other_levels ] ]
  • Expression Stack at time t = [ [ ], [ [ function call with name: "len" and arguments: args, ys ], rest ] ]
  • the element at index idx of stack (Python Object Store at time t) = Python list entries

then Value Stack at time (t + 1) = [ [ length of stack entries, next_level ], other_levels ]

For example, suppose that:
  • Expression Stack at time 10 = [ [ ], [ [ len(numbers), [ ] ], [ ] ] ]
  • Value Stack at time 10 = [ [ Python reference 0, [ ] ], [ [ ], [ ] ] ]
  • Python Object Store at time 20 = [ [2, 4, 6], [ ] ]
The Values Stack tells us that the list is in index 0 of the Object Store.

We know that:

the element at index 0 of stack (Python Object Store at time 10) = [2, 4, 6]

Thus, we conclude that:

Value Stack at time (10 + 1) = [ [ length of stack [ 2, [ 4, [ 6, [ ] ] ] ], [ ] ], [ ] ]

Now, we know that:

length of stack [ 2, [ 4, [ 6, [ ] ] ] ] = 3

Thus,

Value Stack at time (10 + 1) = [ [ 3, [ ] ], [ ] ]

Which simplifies to:

Value Stack at time 11 = [ [ 3, [ ] ], [ ] ]

See the full proof here.

Try stepping through the simulator to see how LW Python computes the length of a list.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments