4 ms·
When I’ve been teaching people to program, explaining [0] has been a tragic experience. Starting at [0] is not objectively better than [1]. I actually believe
by AnthonBerg 8y ago
When I’ve been teaching people to program, explaining [0] has been a tragic experience.
Starting at [0] is not objectively better than [1]. I actually believe it is objectively worse.
- explainplease 8y agohttps://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- StavrosK 8y agoI would like to hear your counterarguments to https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E..., which seems to me like a compelling argument.
- saagarjha 8y agoIt is a compelling argument for calculating ranges, where the subscript is the offset into the range, which is useful when your data structure has properties like being backed by continuous memory and being passed around as a pointer to the first element. But when treating subscripting as “give me the nth element” it doesn’t really make sense.
- Renevith 8y agoIt seems to be mostly about math, with the only mention of array indexing being that 0 looks nicer. "starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero" This doesn't seem to be much of an argument so I'm not even sure what to address.
- StavrosK 8y agoHow would you do something like Python's `x[0:10] + x[10:len(x)]` in a 1-indexed way? [1:11] + [1:len(x)+1]? That's uglier.
- int_19h 8y agoYou just make ranges end-inclusive, and then it's x[1:10] + x[11:len(x)]. The best of both worlds is when a language provides both end-exclusive and end-inclusive slice syntax, as in e.g. Nim: x[0..<10] is end-exclusive, and it's very clear that it is.
- StavrosK 8y agoBut then you no longer get back 10-1 elements, you get back 10-1+1.
- thaumasiotes 8y agoSo what? You're going to have off-by-one errors no matter what convention you adopt. Python glosses over it by providing the alternative syntax x[-1] for what you would otherwise have to write as x[len(x)-1]. But it's at least as valid to provide alternative syntax for inclusive ranges. And logically, if the first element of x is x[0], the last element should be x[-0], not x[-1]. Half-open intervals are appropriate when you're thinking of them as subspaces of a continuum. When you're thinking of them as subsequences of discrete elements, closed intervals are usually more natural. Ruby is the only language I know that really seems to have tried to address the problem, providing different syntax for every way you might want to take a subsequence from an array: x[5..8] # elements [5] through [8], including [8] x[5...9] # elements [5] through [9], excluding [9] x[5, 4] # four elements, starting at [5] If you were working in pure, pencil-and-paper mathematics, you'd choose whether to index a particular sequence from 0 or 1 based on what made your formula look nicer. Both are common. But that's not an approach I'd suggest for a programming language.
- int_19h 8y agoOne thing I would add - using negative indices to index from end is a bad idea in general, because you may inadvertently end up with a negative index as a result of a calculation that you expected to always be positive - and then instead of an out-of-bounds error, you get silent wrong behavior. IMO, it's better to have dedicated syntax for index-from-end, as part of the range syntax. Again, Nim does that: x[0..^1] (although unfortunately they didn't make it symmetric).
- deleted 8y ago[deleted]
- AnthonBerg 8y agoEverybody has an intuition of lists or sequences, and the idea of something being “first”, snd that being labeled “1”. When teaching people to program, portraying the idea of “jumping zero steps” into a list to get the first element has been an inefficient embarassment every single time I’ve been present. Re. Dijkstra’s argument: 1: Everybody has an intuition of an ends-inclusive “from x to y”. Everyone would include 2 and 12 if asked to “count from 2 to 12”. 2: What’s wrong with using three dots? What is pernicious about them? 3: Labeling and length calculations always require a shift of one. If we make the ends compatible with thought-free length calculation, then sequence labeling is always off. If we have intuitive sequence labeling then we need to adjust either end label by one to get the length. The latter approach is far more intuitive. Also, one should just ask the damn computer what the length is. That’s what it’s for. Pardon the language. Especially as we will always want to leave behind and tend to leave behind the abject simplicity of hand-calculating the length of our data from the labels of the bump stops; Our data structures may not even support that calculation. So why couple ourselves to that calculation and sacrifice the ability to have sequence labels make sense? And why don’t we just have both? Keep array[0] as first, fine, whatever, and add array|1| as first too.