It is certainly a question of context. I have seen lots of software which are 1-based for strings, like various Basics, spreadsheet formulas, etc. so it looks for me more intuitive to have 1-based indices for strings.
Exactly that.
It is certainly a question of context. I have seen lots of software which are 1-based for strings, like various Basics, spreadsheet formulas, etc. so it looks for me more intuitive to have 1-based indices for strings.
Exactly that.
Maybe you misunderstood the comment; that loop with First + 1
&c. isn’t part of the setup; it is rather the application of some procedure: a difference equation, say, where you’d handle the boundary differently than the interior.
The point is that indexing an array by an enumeration lets you express the solution at a higher level, in terms closer to the problem, rather than at a lower level, where you may have to convert from integer indices to a more natural way of reasoning about the problem. I’ve found it pretty effective whenever I use Ada.
In retrospect this might be a more helpful example:
-- similar setup with Directions, dx, dy, etc.
type Maze_Objects is ( Clear, Monster, Wall, Entrance, Treasure );
Maze: array ( Rows , Cols ) := ( others => Clear );
-- some superb setup
for Dir in Direction loop
case Maze ( Current_x + dx ( Dir ) , Current_y + dy ( Dir ) ) is
when Clear => -- stroll in confidently
when Monster => -- flee screaming
when Wall => -- have to skip
when Entrance => -- check if you have treasure; if so, git; if not, stick around
when Treasure => -- let's grab it!
end case;
end loop;
…and there are doubtless more effective usages.
Whoops, and now that I’ve written all that, I see that you also added:
Well, that was really my point: Ada’s approach makes it easy to do this, so you don’t have to worry about starting at 0 or 1. In fact, you can start on any integer at all, or on no integer: just whatever makes it natural to solve the problem. But your reply makes me think I’ve misunderstood your concern.
Yes, I think my annoyance was with the Ada string libraries expecting you to start at 1. It is interesting when you talk about solutions being closer to the problem. Sometimes this is because the problem is formulated by mathematicians rather than programmers. It amuses me when I find a piece of code that is wonderfully elegant and simple in it’s self explained with mathematics that (to me of course) looks difficult and abstract. I feel sure for a mathematician the reverse is true. Humanity has such a huge variety of people often operating in quite different ways.
Ada does not specify how arrays are stored in memory.
As array bounds are stored with array content, the first array element is certainly not stored at 0A00+0 (in your example). At this address, there might be one of the bounds.
Same answer than point 3.
As @zertovitch said, for arrays, you can use either an index or a an offset, depending on the context.
I also speak from experience. 0 indexing/offset is responsible of many bugs as people naturally (no pun intended) think with 1-based indexing.
When you play Scrabble with your grand mother, you don’t say you have a word starting with a A at offset 0. You say the first letter is a A.
I agree that 0 terminated strings is a bigger source of bugs.
This is a typical conversation between a coder and a S/W eng. I see no reason to have it again. The coder thinks at the low level, in terms of the processor, addresses, and offsets. The S/W eng thinks at the high level, modeling the problem in the software, and so uses the terminology of the problem. This rarely involves addresses and offsets.
This is a very American-centric view of the world. In Korea, until just a few months ago, babies were born at age 1, for instance.
Thinking about these as offsets is wrong. Those are really indexes, meant to represent real-world contexts. When you look at a car in real-life, do you see wheel number 0 or number 1 ? (I see the latter).
This is like a continuation, and it’s also interesting: [2203.15110] The State of Fortran