Skip to section
Foundationsfor rotation-free search
Section 1 of 522% of course
Contents
Chapter 1 Section 1.1 48 min

Part I · Mathematical foundations

An ordered list is more than a bag of numbers

Separate a value from the position that gives it meaning.

Start with four boxes

Imagine drawing four boxes in a row, placing one number in each box. You've just created two distinct pieces of information: the numbers themselves (the values), and the left-to-right locations that hold them (the positions).

a: values above, positions below
70-215273
Animated visual · ManimA value is not its position

The repeated 7s remain distinct because their addresses differ. Unmute for narration.

You might notice the number 7 appears twice, but they're not exactly the same thing. One 7 occupies position 0, while the other sits at position 3. The position tells us where it is, and the value tells us what it is.

Order is information

Take a look at these lists:

u: values above, positions below
100201302
v: values above, positions below
300201102

They contain the exact same three values, yet they're different lists because the values at positions 0 and 2 don't match up. This is the exact same distinction we make in code between an array and a set. A set just tracks whether a value is present, while an array preserves its exact location.

Worked example

Temperatures by hour

Suppose we have an array [12, 15, 14, 10] that records the temperature at four consecutive hours. If we reorder it to [10, 14, 15, 12], we still have the same set of measurements, but we've completely destroyed the information about which temperature happened at which hour. The position carries meaning, even if we don't explicitly write it next to every value.

What makes two lists equal?

Two ordered lists are considered equal only when two things are true: their lengths match, and the values at every corresponding position match. This translates directly into a practical way to check equality in code. First, we compare lengths. If they're different, we can stop immediately. Otherwise, we just walk through from position 0 to the end, comparing each pair.

def same_list(left, right):
    if len(left) != len(right):
        return False
    for i in range(len(left)):
        if left[i] != right[i]:
            return False
    return True

This loop isn't just a hack for implementation. It spells out the mathematical definition of equality: every single position must make the exact same claim in both lists.

Worked example

The first mismatch is all you need

Let's compare [3, 8, 2, 5] with [3, 8, 9, 5]. Positions 0 and 1 match perfectly. But at position 2, we have a 2 on the left and a 9 on the right. Since they don't match, the lists aren't equal. There's no reason to even look at position 3—finding just one mismatch is enough to disprove that they are “equal at every position.”

Check your understanding

Which change preserves the ordered list [4, 1, 4]?

Length and position

The length of a list is just the number of entries it holds. A four-entry list has four valid positions. Since software (and this book) generally starts counting at zero, those positions are 0, 1, 2, and 3.

Check your understanding

Which statement about [6, 1, 6] is correct?

Lists can hold symbols too

A list doesn't have to contain concrete numbers. [a, b, c] is a list of symbols. Using symbols is a powerful trick because it lets us reason about every possible value all at once. We can talk about "swapping the first and last entries" without needing to know what a and c will eventually be.

A position gets its meaning from a contract

The list itself stores values and their order, but it doesn't know what position 2 actually means. That meaning comes from an agreement we make outside the list itself: maybe it's hour 2 in a temperature series, the blue channel in an RGB color, or a specific feature in a vector embedding. We can only meaningfully compare two lists coordinate-by-coordinate if they share this same external agreement.

Retrieval check: state the equality rule from memory

Two lists are equal when they share the same length and, for every valid index i, the value in the first list at i perfectly matches the value in the second list at i.

Pause and explain this page without using the word vector

An ordered list is a row of values where the positions actually matter. The list has a finite length. Because we use zero-based positioning, a list of length D will occupy positions 0 through D−1.

Section summary

  • A list combines values with their specific positions.
  • Repeated values are distinct from one another.
  • The order of items encodes meaning that the values alone can't capture.
  • A zero-based list of length D ends at position D−1.

Repository layer · second pass

What information disappears when we treat a sequence as a bag?

A sequence is a mapping from positions to values. That sentence is deliberately more precise than “a row of numbers.” It says that position 0 has one value, position 1 has another, and so on. Two positions may contain the same value without becoming the same position. The address and the payload are separate pieces of information.

Software engineers already rely on this distinction. Reordering bytes changes an encoded integer; reordering arguments can change a function call; reordering samples changes a signal. Later, an embedding coordinate will have no friendly name, but its position will still identify one learned feature. Preserving position is therefore part of preserving meaning.

Reasoning chain

  1. 1

    Write the valid positions before writing any values.

  2. 2

    Associate exactly one stored value with each position.

  3. 3

    Compare two lists position by position, not by sorting them.

  4. 4

    Call the lists equal only when their lengths and every positional association agree.

Worked trace

A repeated value does not erase its addresses

  1. Start with A = [7, 4, 7].
  2. The first 7 is A[0]; the second is A[2].
  3. Swapping positions 0 and 1 gives [4, 7, 7].
  4. The multiset of values is unchanged, but two position-to-value associations changed.

Result. The original and reordered lists are different even though their value counts match.

Executable lens · Python

Make the hidden state visible

values = [7, 4, 7]
for position, value in enumerate(values):
    print(position, value)

assert values[0] == values[2]
assert 0 != 2  # equal payloads, different addresses

Retype this example, predict each intermediate value, and then change one input that touches a boundary.

Misconception clinic

Tempting mistakes

  • “The two 7s are the same entry.” They are equal values stored at different positions.
  • “Length 3 includes position 3.” Zero-based positions stop at 2.

Retrieval and transfer

Close the book first

  1. Invent two different length-four lists containing exactly the same values.
  2. Explain whether [a, b] and [b, a] can ever be equal, and state the condition.
  3. Describe one software format in which reordering fields changes meaning.