Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor.

It isn't usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a "1" just seems obviously superior. I'm sure there is a culture that counts their first finger as 0 and I expect they're mocked mercilessly for it by all their neighbours. I've been programming for too long to appreciate it myself, but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.



I've heard the statement "Let's just see if starting with 0 or 1 makes the equations and explanations prettier" quite a few times. For example, a sequence <x, f(x), f(f(x)), ...> is easier to look at if a_0 has f applied 0 times, a_1 has f applied 1 time, and so on.


0-based indexing aligns better with how memory actually works, and is therefore more performant, all things being equal.

Assuming `a` is the address of the beginning of the array, the 0-based indexing on the left is equivalent to the memory access on the right (I'm using C syntax here):

  a[0] == *(a + 0)
  a[1] == *(a + 1)
  a[2] == *(a + 2)
  ...
  a[i] == *(a + i)
For 1-based indexing:

  a[1] == *(a + 1 - 1)
  a[2] == *(a + 2 - 1)
  a[3] == *(a + 3 - 1)
  ...
  a[i] == *(a + i - 1)
This extra "-1" costs some performance (through it can be optimized-away in some cases).


But then again Fortran proceeded C, is known for being very performant, and is 1-based by default.


The comment you are replying to essentially said exactly that:

> but always assumed it traces back to memory offsets in an array rather than any principled stance because 0-counting sequences represents a crazy choice.


> Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor

So you claim this is just an appeal to authority and as a rebuttal you give appeal to emotion without being an authority at all?

> the 1st element of a sequence being denoted with a "1" just seems obviously superior

> I'm sure there is a culture that counts their first finger as 0 and I expect they're mocked mercilessly for it by all their neighbours

> 0-counting sequences represents a crazy choice

5G chess move.


> The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor.

Zero based indexing had nothing to do with Dijkstra's opinion but the practical realities of hardware, memory addressing and assembly programming.

> I'm sure there is a culture that counts their first finger as 0

Not a one because zero as a concept was discovered many millenia after humans began counting.


For math too, 0-based indexing is superior. When taking sub-matrices (blocks), with 1-based indexing you have to deal with + 1 and - 1 terms for the element indices. E.g. the third size-4 block of a 16x16 matrix begins at (3-1)*4+1 in 1-based indexing, at 2*4 in 0-based indexing (where the 2 is naturally the 0-indexed block index).

Also, the origin is at 0, not at 1. If you begin at 1, you've already moved some distance away from the origin at the start.


Just speaking anecdotally, I had the impression that math people prefer 1-based indexing. I've heard that Matlab is 1-based because it was written by math majors, rather than CS majors.


Indeed. I was going to point out that mathematicians choose the index based on whatever is convenient for their problem. It could begin at -3, 2, or whatever. I've never heard a mathematician complain that another mathematician is using the "wrong" index. That's something only programmers seem to do.


Yes but I think it might be just habit, and it's exactly in matlab that dealing with for loops over sub matrices is so annoying due to this


In mathematics, if it matters what index your matrix starts on then you're likely doing something wrong.

Besides, in the rare cases where it does matter you're free to pick whichever is convenient.


Zero-based counting works better with modular arithmetic. Like

  arr[(i++ % arr.length)] = foo;
Is certainly nicer than the equivalent in one-based subscripting

  arr[(i++ % arr.length) + 1] = foo;
(The above is actually wrong, which helps the idea)

I'll concede that it's not all that significant as a difference, but at least IMO it's nicer.

Also could argue that modular arithmetic and zero-based indexing makes more sense for negative indexing.


But on the other hand,

  last := vector[vector-length(vector)];
is nicer than

  last := vector[vector-length(vector) - 1];
so in the end I'd say 'de gustibus non est disputandum' and people who prefer 0-based indexing can use most languages and I can dream of my own.


That's arguably one of the only downsides of zero-based, and can be handled easily with negative indexing. Basically all indexing arithmetic is easier with zero-based.


Using an array as a heap is also easier with 1-based indexing:

  base-element := some-vector[i];
  left-child := some-vector[i * 2];
  right-child := some-vector[i * 2 + 1];
where the root element is `some-vector[1]'.


Yes, negative indexing as in e.g. Python (so basically "from the end") can be incredibly convenient and works seamlessly when indexes are 0-based.


Not quite seamlessly, unfortunately.

`l[:n]` gives you the first `n` elements of the list `l`. Ideally `l[-n:]` would give you the last `n` elements - but that doesn't work when `n` is zero.

I believe this is why C# introduced a special "index from end" operator, `^`, so you can refer to the end of the array as `^0`.


So you're saying a negative index value should work like a count of elements to return and not an index?

Then you couldn't do thing like l[-4:-2] to get a range of elements which seems slightly useful.


No - negative indexing is fine as it is. You just need to be careful about the special case of negative zero.


> Yes, negative indexing as in e.g. Python (so basically "from the end") can be incredibly convenient and works seamlessly when indexes are 0-based.

I'd claim 0-based indexing actually throws an annoying wrench in that. Consider for instance:

    for n in [3, 2, 1, 0]:
        start_window = arr[n: n+5]
        end_window = arr[-n-5: -n]
The start_window indexing works fine, but end_window fails when n=0 because -0 is just 0, the start of the array, instead of the end. We're effectively missing one "fence-post". It'd work perfectly fine with MatLab-style (1-based, inclusive ranges) indexing.


What culture would that be? Because I was under the impression that counting "nothing" generally makes little sense in most of the practical world.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: