Live data from Hacker News

Why numbering should start at zero (1982)

cs.utexas.edu

61–70 of 72 posts

Re: Why numbering should start at zero (1982)

#61

Maybe indexing should be considered harmful. We're living in the age of really smart compilers anyways. It's time to move on. Elixir sets the example - do everything with TCR, provide an Enumerable interface, and beyond that use map/reduce.

Fits perfectly with Dana Scott's suggestion "a trend towards the removal of explicit parameters."

https://github.com/hypotext/notation#mathematical-notation-p...

Re: Why numbering should start at zero (1982)

#63
post #56

So random question because I know I would get this wrong in an interview, but would a 0 based indexing system have fewer assembly language instructions to calculate a memory offset than a 1 based system?

Nope, not in most architectures. In a 0-based indexing system, the base pointer points to the first element of the array. In a 1-based indexing system, the base pointer points to the slot in memory that isone spot behind the first element in the array. This is how Julia (and Ada) does arbitrary indexing.

Re: Why numbering should start at zero (1982)

#64

Stan Kelly-Bootle's famous bon mot on the subject: "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."

That's a genius way to avoid fence post errors (http://www.dsm.fordham.edu/~moniot/Opinions/fencepost-error-...).

Re: Why numbering should start at zero (1982)

#65
post #7

Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops being an advantage. - Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me. I commit less off-by-one errors with 1-based, and I don't have to double-che…

> Starting at 1 is more intuitive, and therefore IMO better. This is totally subjective. Starting at 0 is more intuitive to me. My intuition is very simple. `x` is the name of some memory location. `x[k]` is the location `k` spaces away from `x`.

Having reached introductionary programming to university studendts, I would say that it’s subjective for the individual but still generally true that 1-based indexing is more intuitive.

Re: Why numbering should start at zero (1982)

#66
post #7

Starting at 1 is more intuitive, and therefore IMO better. Regarding some of the advantages of zero-based: - Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops being an advantage. - Indexing cyclically using modular arithmetic. Yes, this is an advantage. Albeit a rare one for me. I commit less off-by-one errors with 1-based, and I don't have to double-che…

> Indexing backwards from the "end". Your language can always add an `end` keyword like Matlab does, and this stops Having used Matlab a bunch, in practice this sucks, because “end” is treated as this weird special case in the language grammar, and many reasonable and convenient expectations of syntax that should work turn out not to. Folks writing complicated Matlab projects end up needing to work around it, and the…

Do you have any examples? In all my years of teaching and working with matlab I have literally never seen any student or code that showed such issues so I am very curious?

Re: Why numbering should start at zero (1982)

#67
Once upon a time when I was young and foolish, I was doing something [1] in C++, which has 0-based arrays. There were a lot of places in the code where 1-based arrays would have made the code quite a bit clearer, but a lot of places where 1-based arrays would have made it much less clearer.

I wanted the best of both worlds, and so overloaded the () operator so that if arr is an array, then arr(i) = arr[i-1].

This worked reasonably well, especially for arrays where it was always clearer to go 1-based, or arrays where it was always clearer to go 0-based, so that the array was always accessed with the same operator.

The only places it was questionable whether or not it made the code clearer was where I used both in the same section of code. E.g., something like b = a[i] + a(i) is arguably less clear than b = a[i] + a[i-1] or b = a(i+1) + a(i).

[1] I think it was implementing an arbitrary precision integer library using algorithms from TAOCP, but I don't remember for sure.

Re: Why numbering should start at zero (1982)

#68

Earlier quoted context omitted.

No, I don't want form of 1 based indexing at all :) an array with elements at index 0,1,2,3 has size 4

And the degree on a polynomial means the highest index of a non-0 coefficient. I don't see how you can view calling a linear polynomial degree 2 an example of 0 based index.

Easy: the constant part of the polynomial is x^0.

Re: Why numbering should start at zero (1982)

#69

The discussion of how to denote the bounds of a sequence may be missing one consideration: Intuition from language. When speaking we articulate a range by stating the first and last permissible value. E.g. "Pick a number between one and ten". My point is understanding the length of the sequence is rarely as important as quickly grasping its bounds.

But when we say "between 1 and 10", the bounds are 1 and 10, but the length of the sequence is - as far as I can count my fingers - also 10.

Re: Why numbering should start at zero (1982)

#70

The discussion of how to denote the bounds of a sequence may be missing one consideration: Intuition from language. When speaking we articulate a range by stating the first and last permissible value. E.g. "Pick a number between one and ten". My point is understanding the length of the sequence is rarely as important as quickly grasping its bounds.

But when we say "between 1 and 10", the bounds are 1 and 10, but the length of the sequence is - as far as I can count my fingers - also 10.

I seem to remember using this in early versions of Visual Basic:

    Dim myArray(1 to 10) As Integer
Don't know if it still works, though.
Post reply on HN