Live data from Hacker News

Indices Point Between Elements

blog.nelhage.com

51–60 of 66 posts

Re: Indices Point Between Elements

#51
post #42
post #39

He undermines his own argument right out of the gate. When he shows the part about which number should be used for the last element in the range there's still two valid choices, you could choose to stop at 3 with the understanding that the element to the right of that is the last element to be selected, or to stop at 4 with the understanding that in this context your indexing is "special" and you're actually selectin…

If you treat the indices as between elements and include all elements that are between the start and end elements, there is only one reasonable choice. It would be more "special" to include elements that are outside of the specified range (i.e., the element to the right of the final index).

Yeah, but I think that orclev was saying that you have a rule at the beginning that the element to the right of the index is important and then in ranges, the element to the right of the index is know longer important.

One way or the other, someone needs to know the same number of rules in order to understand how the indices work.

Re: Indices Point Between Elements

#52
post #10

Earlier quoted context omitted.

I remember something about the ground floor in the UK buildings not being "Floor 1" like it is in the United States. Actually, that's perfectly explained with your offset vs. index terminology. In some countries, the floor number is an index within the array of floors. In others, it's an offset from the ground.

Oh, I know it's perfectly explained by that, but using an example of building floors has cultural idioms. Ruler measurements and birthdays seem to be more universal.

Ah, ok.

In that case, wouldn't the floors actually be a good real-world example to students? It's a case where the index vs. offset convention seems to be split roughly 50%/50% around the world.

If people can't agree about which way is better for numbering floors, it's no surprise that number-crazy programmers can't agree about numbering a whole lot of other things :)

Re: Indices Point Between Elements

#53
post #48
post #2

The visuals are definitely valuable in explaining this. It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1. When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-ba…

> You're in the first year before your first birthday, being zero (whole) years old. I would argue that your "first birthday" is, in fact, the day you are born—your birth day. The thing that happens for the first time a year later, is the first anniversary of your birth day.

And I'd say that the word "birthday" is defined to mean "anniversary of the day you were born"

Re: Indices Point Between Elements

#54
post #22

I've found this a helpful way of explaining ranges in Python to students, it also makes Python's negative indices understandable for the same reason. But it is contextual. When it comes to languages like C, where arrays are more directly mapped to pointers and memory layout, I've found it better to talk about pointers, and allow people to derive the behavior that way. Either way, I'd be careful of trying to claim tha…

The Python documentation use this representation since a long time.

https://docs.python.org/2/tutorial/introduction.html#tut-str...

Re: Indices Point Between Elements

#55
post #12
post #2

The visuals are definitely valuable in explaining this. It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1. When talking about this with other programmers, I've discovered that a lot of the issues/confusion could be avoided by consistent use of terminology: Offsets/offsetting always being zero-based and indexes/indexing always being one-ba…

The birthday analogy is a good one, I'll have to keep that in mind. Your actual "birthday" being the points between year elements, your "age" as the offset from birth. As to the point about floor numbering, the situation can be a little confusing in Canada. Some buildings label in the US style, labeling stories [1st, 2nd, 3rd] while others label in the UK style as [Ground, 1st, 2nd]. We also often mix them and you'll…

[Lobby/Ground, 2nd, 3rd] is common in the US too

Re: Indices Point Between Elements

#56
Once at a party I accidentally started an argument about which way toilet paper should hang from the roll (front or back) by mentioning how silly it was that people would argue about such a trivial matter.

Re: Indices Point Between Elements

#57
post #23

Earlier quoted context omitted.

Memory addresses are exactly analogous to array indices, and suffer from exactly the same semantics issues. After all, low-level memory is just an array of bytes. We do have a convention that dereferencing a memory address returns the 8 bits to the right of that address. We've even optimized our hardware for that convention. But that's just a convention of the dereference operation; it's not fundamental to the addres…

> We do have a convention ^ there, semantic issue resolved specific languages might reuse the word array for abstracting underlying optimizations, but calling array an indexed object doesn't really change what an array is, no more than calling fish a dolphin change it from being a mammal also, a pointer is a range only when paired with a type. otherwise a pointer is the index of a cell within the address space, and y…

Gotcha. Perhaps the article would be better off using a word like list instead of array, to avoid the additional semantics that C attaches to that word.

In any case, I think I agree that dereferencing an address should return the byte to the right for the reason that you mention. That's a solid point, and I totally didn't think of that :) That's a really important property of the dereferencing operation.

I still feel like that doesn't make the mental model of memory-addresses-are-gaps-between-bytes any less valuable, though, nor does it mean that abstractions built on top of this memory model need to use the same conventions as the underlying system - that's the point of abstractions, after all :)

Re: Indices Point Between Elements

#58
Isn't this the classic fence post vs fence section analogy? I like this statement from Djikstra: "an element's ordinal ... equals the number of elements preceding it in the sequence"[1] [1]http://c2.com/cgi/wiki?WhyNumberingShouldStartAtZero

Another nice link: http://betterexplained.com/articles/learning-how-to-count-av...

Re: Indices Point Between Elements

#60
post #24
post #22

I've found this a helpful way of explaining ranges in Python to students, it also makes Python's negative indices understandable for the same reason. But it is contextual. When it comes to languages like C, where arrays are more directly mapped to pointers and memory layout, I've found it better to talk about pointers, and allow people to derive the behavior that way. Either way, I'd be careful of trying to claim tha…

I get into this a bit later on, but I think the exact same model applies to pointers: You're much better off in most cases thinking of pointers as pointing at the zero-width points between elements, than at elements themselves.

Isn't it more obvious that the indices are the addresses of the locations if the boxes are arranged vertically? Besides the boxes should contain their values not their address, an address is attached to a box, and to one box only. Better off indeed thinking of pointer existing in completely different boxes and their values as pointing at the zero-width sides of other boxes :)
Post reply on HN