3 ms·
The argument for 0-based indexing with exclusive upper bounds is that hi - lo == len, that 0-length intervals can be expressed without hi < lo, that if you have
by anonymoushn 4y ago
The argument for 0-based indexing with exclusive upper bounds is that hi - lo == len, that 0-length intervals can be expressed without hi < lo, that if you have found the first index at which a predicate is true and the first index at which it is no longer true, those indices themselves are usable as upper and lower bounds, so you may write my_slice[first_true_idx..first_subsequent_false_idx] to get the sub-slice in which the predicate is true continually, that using mod to restrict indices to the valid range is idx % n rather than (idx - 1) % n + 1.
The argument for 1-based indexing with inclusive upper bounds is that binary heaps are easier to write this way. Edit: And that people who are not primarily programmers may find 0-based indexing weird.
- rightbyte 4y ago> Edit: And that people who are not primarily programmers may find 0-based indexing weird Alas, if just our forefathers had called it "offset" instead of "index".
- patrec 4y ago> The argument for 1-based indexing with inclusive upper bounds is that binary heaps are easier to write this way. Nope. The argument for 1-based indexing with inclusive upper bounds is that, to express ranges, exclusive indexes require you to have a bogus just-one-beyond-the-largest-valid-index element and a way to get the successor for every index. This is a giant PITA for anything but integers (MAX_INT, and x+1), which is why every single programming language I can think of also has end-inclusive ranges in some contexts. If you don't believe me try writing regexps with end-exclusive ranges. On the other hand, there is no natural way to express empty ranges with 1-based indexing.
- anonymoushn 4y agoI'd love to learn more about writing regexps with various kinds of ranges. I don't see the issue, but it's very likely that I'm missing something.
- patrec 4y agoHere's what a regexp for a C-style identifier looks like (ranges are inclusive): [a-zA-Z_][a-zA-Z0-9_]* Here's what the same regexp would look like if regexp ranges were end-exclusive: [a-{A-[][a-{A-[0-:_]* Do you see the issue now? Of course if regexps really worked that way, you'd be better off doing this instead: [a-zzA-ZZ][a-zzA-ZZ0-99_] But that specific trick only works because regexp ranges occur in a set union context.
- deleted 4y ago[deleted]
- anonymoushn 4y agoI see, the `a-z` range has an inclusive upper bound. That makes sense and it really should be like that.