Live data from Hacker News

Why do arrays start at 0?

buttondown.email

681–690 of 702 posts

Re: Why do arrays start at 0?

#681
post #570

Earlier quoted context omitted.

That sounds actually great to avoid any ambiguity. At the cost of a bit of verbosity, though.

that's actually good verbosity, because the intent is very clear using this method, and multiple people can read the code and unambiguously identify the intent without having to talk to the original author. This type of verbosity makes sense in a big organization where the left hand doesn't talk much to the right hand much, so communication naturally evolves to happen at the code level.

Indeed.

I like for example using enum types to ensure that what’s actually passed is the expected value even though fundamentally the semantics do not need to be much more complicated than integer values. There could be the same thing with a distinction between offset and indices as two different integer numerical types to avoid any ambiguity.

Re: Why do arrays start at 0?

#682
post #607

Earlier quoted context omitted.

An array of length 0 starts at 0 and ends with 0

> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0". There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it. ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-st…

The programming languages that I use most often can all represent empty arrays just fine. Of course, indexing into them is an error. But defining them is commonplace, and not being able to define them would make a lot of my code a lot more complicated.

Re: Why do arrays start at 0?

#683
post #491

Earlier quoted context omitted.

It would be nice if this was the case. In grad school I taught in a building that was on a hill and had once been separate buildings so different entrances were on different floor and the transition between the former separate buildings was 4 stairs. So there was a floor with rooms numbered 1xx and 0xx. There was also a floor below the 0xx floor. They just numbered it 00xx. And you could enter the building on any of…

Could this be Dwinelle Hall at UC Berkeley?

nope. IU

Re: Why do arrays start at 0?

#684
post #596

Earlier quoted context omitted.

when I count apples I happily start with one. it's "natural" to label the first thing "1". or how would you count, say, 3 Apples? or three legionaries?

[0, 1, 2] would be how I would refer to my 3 apples. Each apple is identified by the number of apples that precede it. I don't think it's inherently more natural to start from 1, just conventional. Disregarding history/convention, I think it would be more natural to use the lowest available natural number. Back in Roman times, the lowest natural number that people were aware of was "1", so obviously they started coun…

The romans weren't the only people who knew how to count back then... And how many of the other (presumably arbitrary) choices for smallest number were zero? And why did it take over one thousand years to come up with ordinal "zeroth" after we knew about cardinal "zero"?

> It seems that when labeling ... number ... that precede it, plus one.

I don't think so. It's the number that you have counted once you've counted that one.

Re: Why do arrays start at 0?

#685
post #660

Earlier quoted context omitted.

A million, perhaps. But you're talking about adding one instruction to every single data access. Plus your code bloats up with all those extra dec instructions. BCPL and C were created for departmental and lab computers like the pdp-9. Note, for example that the mighty 6502 succeeded in the market because its designers had heard from customers that the $100 price for the 6800 was too much, so they removed some instru…

> But you're talking about adding one instruction to every single data access. No, you misunderstood. You subtract one from the pointer at allocation time. You don’t add an instruction every time you access.

And even if it was an instruction for every access, memory access is much more expensive than and add (at least nowadays -- I don't know if that's always been true).

Re: Why do arrays start at 0?

#686
post #491

Earlier quoted context omitted.

Could this be Dwinelle Hall at UC Berkeley?

nope. IU

Cool, Dwinelle at Berkeley is a fascinatingly bizarre building for seemingly very similar reasons, and (if I remember correctly) also with pretty much the same weird numbering system plus tiny staircases to adjust for mismatched heights between floors!

Re: Why do arrays start at 0?

#687
post #684

Earlier quoted context omitted.

[0, 1, 2] would be how I would refer to my 3 apples. Each apple is identified by the number of apples that precede it. I don't think it's inherently more natural to start from 1, just conventional. Disregarding history/convention, I think it would be more natural to use the lowest available natural number. Back in Roman times, the lowest natural number that people were aware of was "1", so obviously they started coun…

The romans weren't the only people who knew how to count back then... And how many of the other (presumably arbitrary) choices for smallest number were zero? And why did it take over one thousand years to come up with ordinal "zeroth" after we knew about cardinal "zero"? > It seems that when labeling ... number ... that precede it, plus one. I don't think so. It's the number that you have counted once you've counted…

How do you write zero in Roman numerals? Answer: you can't. Even though they used their number system for adding amounts of money, they hadn't figured out "cardinal zero" yet.

It was introduced into western mathematics through Fibonacci in the 1200s at the same time that Hindu-Arabic numerals were adopted, which use "0" as a placeholder (compare this to earlier Greek numerals which work similarly to the system we use today but without placeholders and using different sets of symbols for the different places—and of course no way of representing zero).

Re: Why do arrays start at 0?

#688
post #607

Earlier quoted context omitted.

An array of length 0 starts at 0 and ends with 0

> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0". There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it. ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-st…

Allowed in Rust:

https://play.rust-lang.org/?version=stable&mode=debug&editio...

it's an array of length 0, but it can't be "used" because it has a length of 0 so it occupies no RAM

Re: Why do arrays start at 0?

#689
post #620
post #605

Earlier quoted context omitted.

In college, I optimized the array removal routine from O(N^2) to O(N) by using INVARIANTS But it probably ran in like 100us anyway

Ugh, invariants... Anyway, complexity stays relevant, no matter what language you use. That's the real deal.

It only stays relevant if the JavaScript is too slow. In the case of removing two elements from an array of length 8, it's not too slow to remove them one by one since, again, it runs in microseconds

Re: Why do arrays start at 0?

#690
post #118
post #70

Earlier quoted context omitted.

As the article implies, Dijkstra was specifically wrong about FORTRAN, as defined at the time — the '77 standard, when it was still only conscionable to SHOUT 6 alphamerics. “… you probably know that arrogance in computer science is measured in nano-Dijkstras.” — Alan Kay

"This quote keeps on showing up out of context." — Alan Kay https://news.ycombinator.com/item?id=11799963

Well, I did hear it in context, and have also heard him talking about concocting aphorisms. Kay also said “He did like to pull chains …”
Post reply on HN