TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Indices Point Between Elements

133 pointsby nelhagealmost 10 years ago

20 comments

thwartedalmost 10 years ago
The visuals are definitely valuable in explaining this.<p><i>It used to be popular, and still is in some circles, to debate whether programming languages ought start array indexing at 0 or 1.</i><p>When talking about this with other programmers, I&#x27;ve discovered that a lot of the issues&#x2F;confusion could be avoided by consistent use of terminology: Offsets&#x2F;offsetting always being zero-based and indexes&#x2F;indexing always being one-based.<p>Using rulers and birthdays also helps to explain differences. You&#x27;re in the first year before your first birthday, being zero (whole) years old.<p>To make matters potentially more confusing, culturally, I remember something about the ground floor in the UK buildings not being &quot;Floor 1&quot; like it is in the United States.<p><a href="http:&#x2F;&#x2F;www.hacker-dictionary.com&#x2F;terms&#x2F;fencepost-error" rel="nofollow">http:&#x2F;&#x2F;www.hacker-dictionary.com&#x2F;terms&#x2F;fencepost-error</a>
评论 #10112460 未加载
评论 #10111616 未加载
评论 #10111637 未加载
评论 #10111677 未加载
评论 #10112913 未加载
评论 #10111928 未加载
Someonealmost 10 years ago
See also <a href="https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;users&#x2F;EWD&#x2F;transcriptions&#x2F;EWD08xx&#x2F;EWD831.html" rel="nofollow">https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;users&#x2F;EWD&#x2F;transcriptions&#x2F;EWD08xx&#x2F;E...</a> (&quot;Why numbering should start at zero&quot;)
评论 #10111543 未加载
surrealizealmost 10 years ago
People have taken different approaches to this in bioinformatics for numbering intervals of dna bases in chromosomes. I think this approach is catching on, though. In that realm, it&#x27;s important to speak unambiguously about insertions and deletions, and the &quot;interbase&quot; mental model makes it a lot clearer.<p>edit: Probably the most popular genome browser, based at UC Santa Cruz, uses this zero-based, half-open numbering internally. But, at some point in the past, biologists developed an expectation that numbering would be 1-based. So the Santa Cruz genome browser actually adds 1 to the start position of everything for display purposes.
评论 #10112197 未加载
carapacealmost 10 years ago
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.
评论 #10113689 未加载
sagoalmost 10 years ago
I&#x27;ve found this a helpful way of explaining ranges in Python to students, it also makes Python&#x27;s negative indices understandable for the same reason.<p>But it is contextual. When it comes to languages like C, where arrays are more directly mapped to pointers and memory layout, I&#x27;ve found it better to talk about pointers, and allow people to derive the behavior that way.<p>Either way, I&#x27;d be careful of trying to claim that &#x27;this is what it <i>is</i>&#x27;, rather than &#x27;here is a way to remember it&#x27;.
评论 #10112042 未加载
评论 #10113244 未加载
orclevalmost 10 years ago
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&#x27;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 &quot;special&quot; and you&#x27;re actually selecting the element to the left of the index.
评论 #10112726 未加载
perlgeekalmost 10 years ago
When you talking about where zero-width regexes match, this mental model certainly helps, and I find it consistent otherwise too.
评论 #10112055 未加载
DougBTXalmost 10 years ago
Further notes about half open intervals and why starting at zero is a good idea: <a href="http:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;users&#x2F;EWD&#x2F;ewd08xx&#x2F;EWD831.PDF" rel="nofollow">http:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;users&#x2F;EWD&#x2F;ewd08xx&#x2F;EWD831.PDF</a>
bchalmost 10 years ago
There is a reference to Emacs in the OP, but not directly to what immediately sprung up in my mind[0]. In Emacs, it has a cursor (the block) and a &quot;point&quot; which is described as an imaginary spot at the left edge of the cursor - the point is what&#x27;s important when one is indexing data (ie: setting a mark and selecting a bunch of text). The model fits for lots of things... Unfortunately for me, tmux highlighting does <i>not</i> follow this model.<p>[0] <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;manual&#x2F;html_node&#x2F;emacs&#x2F;Point.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;emacs&#x2F;manual&#x2F;html_node&#x2F;emacs&#x2F;Po...</a><p>edit: clarify Emacs reference in OP
MrManateealmost 10 years ago
I have sometimes wondered whether it would be useful to have two different types for indices, depending on if we are indexing the elements themselves or the &quot;gaps&quot; between them. Let&#x27;s say I&#x27;m thinking about some language like Haskell, where types are already a big deal.<p>That way the compiler would be able to tell if I accidentally mixed the two. Every conversion would have to explicit: for example, there might be two functions, &quot;before&quot; and &quot;after&quot;, that take a gap index, and return an element index.<p>I think I might actually enjoy programming this way, but perhaps others would find it needlessly bureaucratic.
curiousDogalmost 10 years ago
Isn&#x27;t this the classic fence post vs fence section analogy? I like this statement from Djikstra: &quot;an element&#x27;s ordinal ... equals the number of elements preceding it in the sequence&quot;[1] [1]<a href="http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?WhyNumberingShouldStartAtZero" rel="nofollow">http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?WhyNumberingShouldStartAtZero</a><p>Another nice link: <a href="http:&#x2F;&#x2F;betterexplained.com&#x2F;articles&#x2F;learning-how-to-count-avoiding-the-fencepost-problem&#x2F;" rel="nofollow">http:&#x2F;&#x2F;betterexplained.com&#x2F;articles&#x2F;learning-how-to-count-av...</a>
marijnalmost 10 years ago
And this is why JavaScript&#x27;s `lastIndexOf` method gets it wrong, wrong, wrong. (`&quot;foo&quot;.lastIndexOf(&quot;o&quot;, 2)` yields 2, whereas I, and probably a lot of other people, would expect it to be 1, with the search starting at the space between element 1 and 2)
Camilloalmost 10 years ago
This is the mental model I use.
spartan37almost 10 years ago
Relevant <a href="https:&#x2F;&#x2F;github.com&#x2F;vr6&#x2F;blog&#x2F;blob&#x2F;master&#x2F;Zero-One.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vr6&#x2F;blog&#x2F;blob&#x2F;master&#x2F;Zero-One.md</a>
colandermanalmost 10 years ago
This is the same model used by the Cairo graphics library and the HTML5 Canvas (albeit in two dimensions). All the same benefits apply when you&#x27;re addressing pixels as elements of an array.
ape4almost 10 years ago
We need a different name for this. We&#x27;re too used to &quot;index&quot;. If the name is &quot;Foo&quot; then we can have fooSubString(i,j);
pshcalmost 10 years ago
This echoes the way unums work in &quot;The End of Error&quot;. (Recommended read by the way.) Good way of thinking about ranges.
ameliusalmost 10 years ago
Ugh. Please no.<p>This only adds to the confusion, as now there are 2 ways in which indices can be interpreted.
评论 #10112163 未加载
评论 #10111902 未加载
maakualmost 10 years ago
Great teaching tool, thank you.
anon5446372almost 10 years ago
Some people spend too much time overthinking simple concepts...
评论 #10113394 未加载