From where should the index start?
-
BobJanova wrote:
It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.
How? See my comment above, by reducing the number of steps, you produce more efficient programs.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Nagy Vilmos wrote:
by reducing the number of steps, you produce more efficient programs
Who cares? :confused:
-
Did you read the rest of the post? As I said there, "Only in the case of indexing a fixed-memory array directly by specifying a calculated index would there be an extra operation". And most of the times you have to do that now are because of a language deficiency. And finally one instruction is so trivial that who cares anyway (on a 4Ghz machine it will cost you 0.25ns, and that's if getting the data your algorithm is working on onto the core cache isn't the limiting factor already) – language design should not be constrained by such trivia.
Where we can, we should not be using indexes today. When we do, need to respect how things are stored.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Do some research Bubba. The point of OO is to provide another level of abstraction from the machine. The amount of code in an OO language based solution is much greater than for a traditional procedural language. However by using classes, the amount of code in one place can be reduced and, more importantly, the code is more tightly connected to the data. The languages are abstracting away the complexity of pointers, but every object is a pointer, we are helped by the fact that a lot of the handling that we had to do by hand in a language like C is now done 'for free' by C# for example. This does not get away from the fact that, at some point, the instruction has to be reduced to finding the memory address containing the item you need. By using zero based arrays, there is one calculation removed, a single machine step. So, this goes back to my original comment. Zero based makes sense because it is better once everything is compiled down. Now which part don't you understand?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.
-
Nagy Vilmos wrote:
Which is easier to compute?
Neither.
p+(n-1)*s
equals(p-s)+n*s
where(p-s)
is a constant, just likep
is, so all it takes is a different origin, the computational effort is exactly the same. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
I disagree that
(p-s)
is constant. It does not changemuch
, but asp
can be changed, re-assign the array for instance, the pointer cannot be taken as being to a constant place.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
It's pretty difficult to work without lists, and you need to get things out of a list. Unless you count '1', '2' etc as 'meaningful symbols' in which case you still have indexing really. You can help a lot by having enumerations (foreach doesn't require indexing) and array-based functionality (so you can add two vectors without needing to loop around them), but a lot of real world problems would be much more difficult without being able to look things up by index in arrays or collections.
BobJanova wrote:
foreach doesn't require indexing
You don't know what's going on in the background.
-
This makes no sense. Counting and indexing are completely different things – even the 0 based computing world accepts that a collection of things A, B and C has 3 items, even if it calls them '0', '1' and '2'. The fact is if a person is counting objects they do use 0, it is just omitted. That doesn't mean anything either. A count of 0 is when there are no things to count. But if there are 6 apples, it doesn't matter what indexing system you use, or how many you count at once, there are always 6. What 'indexing' is about it labelling. Ask anyone to label those apples with numbers and everybody (unless they're being deliberately silly) will call them '1', '2', '3', '4', '5' and '6'. Look at things which are labelled in the real world: bases on a rounders/baseball field, quarters or halves in a sports match, aisle numbers in the supermarket. Even in the computing world this is often true; look at the MRU on Excel or Word, it numbers your recently used files from the first (1) up. Well starting the index at 1 for computers is inefficient As I'm discussing with Nagy above, technically true in a small number of cases (many of which are due to language deficiencies regarding array operations in C family languages) but so small as to be irrelevant as a consideration in the modern world. The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.
You are missing the step that computers have to do. You placed those apples somewhere no? How is the computer suppose to know where that is? We as humans SEE them. A computer needs the reference. That reference is the what??? Thats right, the 0th index. If you switch it you are now forcing the computer to subract 1 off of every access.
Apples[] myAppleCollection; //In human terms the apple collection sitting right in front of me
By using the "Label" as you put it as the accessor the computer is able to be more efficient and access the first apple because it is the what? Yes, now you are getting it. The 0th index. If I want the second apple I reference it by adding the size of the apple to the collection. You can say this is the 'old' way of doing it, but under the hood it will always do it this way. It has to. If you can not grasp it you should not program.
BobJanova wrote:
The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.
This may be the question but your answer is not complete. I want one that allows people to speak easily but at no cost to performance, i.e. the person still needs to understand what is being compiled. You can't get around that. I don't know the details of how a microwave works, but I know not to stick my fork in it. I also know that it will cook my cat (did anyone see that article.. I mean WTF?). If you do not understand the BASE technology of what you are using, you should not use it.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Collin Jasnoch wrote:
In human languages we tend to use 0 explicityly.
I think you mean implicitly.
Collin Jasnoch wrote:
Example, I want to order 5 Chicken strips.
Damn, now I'm hungry. Is it lunch time yet?
-
If it were to be a clean start, I'd definitely have an index start with '1'. As to why, the reason is simple, it's they way we count since we're 2 years old. How does a small kid learn his numbers? I find it hard to imagine some little boy counting 0, 1, 2, ... We start at 1, and do so even when we are grown ups. Zero based indexing is just a legacy of array pointers and offsets. It doesn't help in any useful way in modern languages, it only makes things more confusing, its cumbersome to referrence the last item of an array and it doesn't agree with how we normally count things; when you go to a grocery store and you ask for 10 apples, you expect to get ten apples, them being apple nº1, apple nº2, ... , apple nº10, definitely not apple nº0, ..., apple nº9. So why when you "buy" a 10 item array you get the latter and not the former? WTF?!?
gumi_r@msn.com wrote:
it's they way we count
How many elephants are on the moon? Did you count them? You may not think about it, but you always count from zero.
-
As I posted before you'r reasoning IMHO is not solid. The fact is that array[0] is a meaningful item in a zero based indexing system, while in our everyday life, 0 is the contrary; it can be omitted, as you well say, precisely because it is not meaningful, we live in a 1-index based world like it or not. And I absolutely disagree with the supposedly overhead paid when the compiler has to correctly interpret array[1] as the first item of the array (that is, offset zero in the uderlying pointer) when it spits out machine code, CIL or what have you. Why should there be any performance issue at all? Let the compiler transform everything to zero based indexing that the computer natively understands at compile time (we may have a 1 ms compile time overhead in there somewhere...). Or should we also start writing our programming in assembly code just to make the computers understand us better? Obviously this is of course a hyothetical discussion. We are stuck with zero based indexing and it is after all trivial to understand how it works.
Dynamic allocations make it so that performance hit could go out to runtime. Furthermore, you can NOT say 1ms. It competely depends on the application. In some cases you could shatter the performance down to a crawl (arrays of arrays of arrays of arrays ......) For a simple GUI sure, no loss. But thats why MS has VB. You want a simple GUI that does little computation and can't be maintained with out turning into a gobbldy gook of sludge. Fine, program in VB. For the rest of the world and the programers that understand why it is 0 based and are not the slightest bit confused by it, we will continue to make our applications that out perform anything that could be made by this nonsence 1 based index language.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
Intriguing discussion. :)
BobJanova wrote:
When they select one, you want to generate a report of the details of the record they selected (i.e. records[record_id]).
Erm: what about
DataRow selectedRow = selectionEventArgs.SelectedRow;
?BobJanova wrote:
You've got a network protocol where the last byte of a message (bytes[bytes.length - 1]) is a checksum.
Been there. I submit that the packet can be coded into a memory map (I did this as a union ages ago) to a structure, so I could write
packet.Checksum
OK, granted the packet was a fixed length. :)BobJanova wrote:
One of the parameters to that method in all conventional languages is an 'index'
Yeah, which I find annoying. If the fields within the string are fixed length, then they can be put into a structure. If the fields aren't fixed length, then I submit that the language (you did ask originally, if I was defining a language) should allow for specifying, say, delimiters (which a variable length string of sub-fields would almost always need unless there was some encoding, like in compression, that determines the length) so that again, the programmer would be isolated from using indices. Marc
Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table? OK, granted the packet was a fixed length That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes. Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be. I'm not convinced that every usage of Substring can be replaced so easily. After all, most modern languages have Split (or explode or something else that does the same job) for delimiter-separated fields, and people still use Substring a lot. Don't get me wrong, I agree that reducing the number of places indices (or offsets, as they are in most languages) are necessary is a good thing. I'm a big fan of enumeration and array operations. But I don't think a language without the capability of using them would be practical, particularly when you consider trying to interface with existing systems.
-
BobJanova wrote:
foreach doesn't require indexing
You don't know what's going on in the background.
The point here is that Joe Coder doesn't need to manage the index.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Collin Jasnoch wrote:
When you count Apples you DO start with 0. You used the word APPLE No? In computers that is where 0 starts, The definition. In Human tounge it is an implicit deffinition. Computers must work in concrete terms. You can't change that. Yes you can abstract it. But at a cost of efficiency.
That is a really wierd way to undersant it. We live in a 1 based indexing world. You said it yourself, we have the luxury of being able to omit the 0th element in our life precisely because it means NOTHING to us. Quite the contrary in the programming world, where the 0th element or the nth element are equally meaningful.
That may be a wierd way to understand it, but that is reality. If I tell my child to count the apples and he starts at 1 even though there are no apples he is not grasping mathematics. If there are apples and he starts at 0 he is not grasping efficiency. Computers can not ommit details. They have concrete logic, not fuzzy and everything need be explicityly defined. If that is not there basis then one one of a program with a locked data set could result in a different outcome over time. This would be chaotic and unproductive. And you want this because understanding the 0th index is too hard?
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
-
I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.
Using VB, is the problem there :-D. Seriously in C# it'd be:
for (int i = 0, i < ListBox1.Items.Count, i++) {
ListBox1.Items\[i\].property = stuff;
}
But also you can use foreach construct:
foreach (Object obj in ListBox1.Items) {
o0bj.property = stuff;
}nikunjbhatt84 wrote:
The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same.
So if you don't like indexes, don't use them.
nikunjbhatt84 wrote:
More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.
If you'd actually done any machine code, you'd understand the problem with this statement. Higher level languages abstract mnore and more of the machine away, but saying that means you don't need to understand what is happening is stoopid.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
I disagree that
(p-s)
is constant. It does not changemuch
, but asp
can be changed, re-assign the array for instance, the pointer cannot be taken as being to a constant place.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
it is a simple "translation transformation", i.e. a shift of origin: just make the pointer to an array of some type point to the zero-th element of the array, as is done in C-style languages; whether the zero-th element exists or not is immaterial. For a 1-based array of ints at address 0x1000, p would equal 0x0FFC (assuming a 4-byte int). So when allocating an int array in C, say malloc returns 0x1000 and you want that to point to an int array, subtract 4 and store the pointer. The compiler can do that for you; it also can adjust for size differences when you cast one pointer type to another (say casting a char* to an int*). In fact, the malloc case isn't special, just treat void as a type with size zero! :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table? OK, granted the packet was a fixed length That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes. Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be. I'm not convinced that every usage of Substring can be replaced so easily. After all, most modern languages have Split (or explode or something else that does the same job) for delimiter-separated fields, and people still use Substring a lot. Don't get me wrong, I agree that reducing the number of places indices (or offsets, as they are in most languages) are necessary is a good thing. I'm a big fan of enumeration and array operations. But I don't think a language without the capability of using them would be practical, particularly when you consider trying to interface with existing systems.
BobJanova wrote:
If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table?
Well, a better implementation would be:
Person somePerson = selectionEventArgs.Person;
Address personAddress = Person.Address;One of things I would definitely design into a new language is the ability to utilize foreign key and unique key schema information to reference other tables, and that those tables and their fields could be used directly, at compile time, from the schema definition. No ORM required.
BobJanova wrote:
That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes.
Agreed, but I would provide a mechanism in the language to specify variable length fields with the IOC option to determine lengths at runtime.
BobJanova wrote:
Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be.
Well, by definition, a stream is read sequentially, so indexing shouldn't be done on the stream itself. And again, I'd create the language so that variable length fields can be defined in a structure into which the stream can be read. I'm just trying to think outside the box, how to eliminate indexing completely through language supported syntax and features. Marc
-
You are missing the step that computers have to do. You placed those apples somewhere no? How is the computer suppose to know where that is? We as humans SEE them. A computer needs the reference. That reference is the what??? Thats right, the 0th index. If you switch it you are now forcing the computer to subract 1 off of every access.
Apples[] myAppleCollection; //In human terms the apple collection sitting right in front of me
By using the "Label" as you put it as the accessor the computer is able to be more efficient and access the first apple because it is the what? Yes, now you are getting it. The 0th index. If I want the second apple I reference it by adding the size of the apple to the collection. You can say this is the 'old' way of doing it, but under the hood it will always do it this way. It has to. If you can not grasp it you should not program.
BobJanova wrote:
The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.
This may be the question but your answer is not complete. I want one that allows people to speak easily but at no cost to performance, i.e. the person still needs to understand what is being compiled. You can't get around that. I don't know the details of how a microwave works, but I know not to stick my fork in it. I also know that it will cook my cat (did anyone see that article.. I mean WTF?). If you do not understand the BASE technology of what you are using, you should not use it.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
'First [1st] apple' ... '0th index'. And you're still trying to claim this is a natural approach? The indexer on a collection class is rarely just a memory lookup anyway. In C#/Java, even an array is not just a memory lookup (the implementation is something like
struct array<T> { int l; T* content; }
) so even the 'efficiency' argument doesn't add up once you move away from C (the address in memory is the collection's home plus some constant plus the index/offset anyway, you can just adjust the constant for a different index origin on the collection) – even aside from the fact that the performance hit on pure pointer arrays (even in the few cases it actually happens) is negligible to zero anyway. the person still needs to understand what is being compiled. You can't get around that. How much does the average programmer know about Intel/AMD chip opcodes, graphics memory, keyboard drivers and all the other things that their compiled program interacts with? This, honestly, is nonsense. A good language is there to insulate the programmer from needing to know about all the low level crap that the computer is actually doing. I shouldn't have to care what the compiled result of using a List<T> is in order to make decisions about when to use one. -
In almost all programming languages, the index number of arrays, strings and other starts with 0 (Zero). Now suppose, presently you are going to design a new language and forget that any programming language already exist. Then from where would start the index number? Zero or One or something else? Here I am mentioning the reason behind coming this question in my mind. The problem with Zero based index is faced (a little) in the function of finding position of a string inside another string and/or checking existence of one string into another. When the searched string is found at the first character (index=Zero), the function will return Zero and if it is found elsewhere, it will return that Zero based position, and it is NOT found, it may return -1 (depending on the language). { In PHP, however there is a good function which return "false" if the string is not found. } But what if we just want to check weather a string exist in another string or not? There could be another function for this purpose OR you may need to check 2 conditions, one for its index and second for its existence. Now, if the index starts from 1, the function will return 1 if the searched string found at the first character, and this will make easy for checking existence of one string into another, because if the string is not found, the function can return Zero and thus making the IF condition false. So, what do you think about starting number for index in a language if you are going to be the founder of the language?
From Wiki[^] Advantages [Using 0 based Index] One advantage of this convention is in the use of modular arithmetic as implemented in modern computers. Every integer is congruent modulo N to one of the numbers 0, 1, 2, ..., N − 1, where N ≥ 1. Because of this, many arithmetic concepts (such as hash tables) are more elegantly expressed in code when the array starts at zero. A second advantage of zero-based array indexes is that this can improve efficiency under certain circumstances. To illustrate, suppose a is the memory address of the first element of an array, and i is the index of the desired element. In this fairly typical scenario, it is quite common to want the address of the desired element. If the index numbers count from 1, the desired address is computed by this expression: where s is the size of each element. In contrast, if the index numbers count from 0, the expression becomes this: This simpler expression can be more efficient to compute in certain situations. Note, however, that a language wishing to index arrays from 1 could simply adopt the convention that every "array address" is represented by a′ = a – s; that is, rather than using the address of the first array element, such a language would use the address of an imaginary element located immediately before the first actual element. The indexing expression for a 1-based index would be the following: Hence, the efficiency benefit of zero-based indexing is not inherent, but is an artifact of the decision to represent an array by the address of its first element. A third advantage is that ranges are more elegantly expressed as the half-open interval, [0,n), as opposed to the closed interval, [1,n], because empty ranges often occur as input to algorithms (which would be tricky to express with the closed interval without resorting to obtuse conventions like [1,0]). On the other hand, closed intervals occur in mathematics because it is often necessary to calculate the terminating condition (which would be impossible in some cases because the half-open interval isn't always a closed set) which would have a subtraction by 1 everywhere. This situation can lead to some confusion in terminology. In a zero-based indexing scheme, the first element is "element number zero"; likewise, the twelfth element is "element number eleven". Therefore, an analogy f
-
BobJanova wrote:
foreach doesn't require indexing
You don't know what's going on in the background.
Actually, I do, if the thing I'm foreaching over is an IEnumerable in .Net, I know that it really isn't using indexing in many cases (typically it's a linked list type forward reference chaser). But we're talking about languages here, what is going on in the background doesn't matter. (Obviously, go digging far enough and you find the 0 based memory offsets that the processor architecture uses, but that's not a useful thing to talk about in this context.)
-
Walt Fair, Jr. wrote:
I'd make it specifiable so you could decide in each case what made better sense as the base index.
That seems dangerous. If the system is changing its base index how is a maintainer of the system suppose to understand the logic of the one whom developed it in the previous decade? Yes in some cases you want 1 base. The only time I run into this is if some values are making it to the UI. E.g. I have a grid displaying products. Users understand it as 1,1; 1,2; 1,3 but the system understands it as 0,0; 0,1; 0,2 This is exactly why patterns exist for what we display versus the business logic (MVVM, MVC etc.) Your business logic should NOT be using a modified index as it is leads to confusion in other parts of the system (non viewable). I have seen code where the developers tried to be clever and do these things. You end up with a freaking mess. Keep it consistent across the board.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.
No, I wouldn't be so stupid as to suggest a blanket system wide change. Note that Basic allows that with its OPTION BASE -- another reason not to use BASIC! I'd let it work on an array by array basis, like Algol. For example, when you declare an array, you specify its base array, with a default of 0. Sort of like A[1:10] or B[-2:15] or C[0:19] which would be equivalent to C[20] or some such syntax. I'd have to give it more thought before actually proposing a specification. Requiring that an array start with a specific index in all cases is just an unnecessary limitation. Why can't I make an array that goes from -22 to +46, if it makes my code more intelligible? Obviously it's possible, since we can map indices in most any language right now or use an associative array in the languages that support that structure. The index limitation, in my opinion, is a throw back to earlier times when compilers were more limited. It sounds like your applications and mine are much different. I find a lot of cases where it's convenient to use arrays with indices that don't start at either 1 or 0 and end up having to map them back to 0 or 1 based indices or use a different data structure. It's no big deal, but the question was asked what I would do, and that's what I would do. Pretty much just like Algol. I just don't care for arbitrary, compiler imposed limitations when the compiler can automatically do the mapping and the programmer/developer could use whatever base is most convenient, clear, and logical for the algorithm description.
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
'First [1st] apple' ... '0th index'. And you're still trying to claim this is a natural approach? The indexer on a collection class is rarely just a memory lookup anyway. In C#/Java, even an array is not just a memory lookup (the implementation is something like
struct array<T> { int l; T* content; }
) so even the 'efficiency' argument doesn't add up once you move away from C (the address in memory is the collection's home plus some constant plus the index/offset anyway, you can just adjust the constant for a different index origin on the collection) – even aside from the fact that the performance hit on pure pointer arrays (even in the few cases it actually happens) is negligible to zero anyway. the person still needs to understand what is being compiled. You can't get around that. How much does the average programmer know about Intel/AMD chip opcodes, graphics memory, keyboard drivers and all the other things that their compiled program interacts with? This, honestly, is nonsense. A good language is there to insulate the programmer from needing to know about all the low level crap that the computer is actually doing. I shouldn't have to care what the compiled result of using a List<T> is in order to make decisions about when to use one.BobJanova wrote:
so even the 'efficiency' argument doesn't add up once you move away from C (the address in memory is the collection's home plus some constant plus the index/offset anyway, you can just adjust the constant for a different index origin on the collection)
I will agree that this can make the efficiency argument a moot point, however you just make things more complicated for the purpose of simplicity. That doesn't make sence. The reference to the collection IS the first item. yes you can trick it and build a new reference (minus one) and use that and now you have a 1 based array. Hooray! What is the point. My thought (again) is if you can't understand the purpose of it being 0 based you should not be programming. See my post from Wiki (responed to the OP). There are other reasons we use 0 as well. and I have tried giving human comparisons. The idea that the human languange (any language be it English, Japanese, French or even Arabic), is able to speak with implicit definitions. Computer languages do not because math does not. If this reasoning is a mystery to you, you are simply in the wrong field. Sure sure, you can call yourself a programmer. But please for the love of god stay out of my algorithms! Go ahead tweak som UI stuff, but when it comes to the inner workings clearly you should not stir the pot.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.