From where should the index start?
-
gumi_r@msn.com wrote:
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.You eventually teach children the concept of 0. It is something you must teach them. However, we need not teach computers the concept of 0. The fact is if a person is counting objects they do use 0, it is just omitted. For example, I have 6 apples all lined up. I want to count them. Now I am a smart chap so I can just look and say there are 6 apples. Someone else may need to at the minimum count by 2's (omitting the odds). And yet another will count from 1 to 6 (omitting the 0). The fact is all counting of objects (which is what your comparison is about) start with 0, we just choose to omit for efficiency. Well starting the index at 1 for computers is inefficient because the fact is 0 exists, and the computer knows that. If you start at 1 you now have to proram at the base level speacial instructions for it to understand 0. Why would you do that? So you example of going to the grocery store. What are you going to do when the clerk hands you 1 Orange, 1 watermelon and 1 zuchini?? "Hey I didn't ask for these!" "Oh sorry, you forgot to say 0 of those items..."
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.
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.
-
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?
-
Simply put it should be 0 or it becomes more confusing. Many have pointed out why it is 0 and to not grasp that is then dangerous for you to program. The reasoning is simple. You are working with a system that reconizes 0 implicitly. There is no changing that. The reasoning for this is that 0 does exist. In human languages we tend to use 0 implicitly. This means that unless I say otherwise assume 0. Example, I want to order 5 Chicken strips. The waitor will hopefully not bring me 1 of everything else on the menu. Collections being 0 based indexing are indirectly related to this. Define Chicken stripsBasket (implicityly I have 0 chicken strips but I have my basket) Fill basket with 5 chicken strips Now to access your chicken strips you have a reference to the basket... Not your friends basket... Not the basket on the table next to you. Your basket. For efficiency referencing the basket gets you a chicken strip, unless you wanted to actually eat the basket. By requiring 1 based indexing you are adding a step to the chicken strip access which when compounded with the complexity of systems we create today would have horrible performance (and for no reason). If one does not grasp why computer languages use 0 based indexing (more importantly that they do), one should not be a programmer IMHO.
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.
-
That I honestly would not like at all. I think a bad standard is better than no standard at all. It would be a nightmare if everytime you had to get your hands dirty with some codebase you had to factor in the index base whoever wrote the code decided to use on that given day/project/etc.
-
Iteration requires looking at all the items, sometimes you don't want to do that. For example? You've got a list of purchase records in some form of ID-indexed structure, and presented to the user is a list of their titles. When they select one, you want to generate a report of the details of the record they selected (i.e. records[record_id]). You've got a network protocol where the last byte of a message (bytes[bytes.length - 1]) is a checksum. Various string manipulations as currently done with Substring methods (extracting particular fields from a combined text string of some kind). One of the parameters to that method in all conventional languages is an 'index' (actually an offset in most of them).
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
-
Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address
p
and each item is an offset from there so, for itemn
where each element is sizes
the memory address isp+n*s
. If you want 1 based arrays then the element would be found atp+(n-1)*s
. Which is easier to compute?
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:
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
-
nikunjbhatt84 wrote:
the function can return Zero and thus making the IF condition false.
No, because using zero as false is dangerous. That's why strongly typed languages (such as C#) have an actual bool class. So you don't accidentally mix up using an int as a bool when it was supposed to be an int.
Wjousts wrote:
using zero as false is dangerous
Correct. Zero should be
true
. :-D I further agree that programming languages need a definite boolean type. Back when D was still being ironed out, I was one who argued in favor of a boolean type. It didn't happen, I don't use D. -
gumi_r@msn.com wrote:
From your post you seem to be implying we should just do without levels of abstraction.
Not at all. I am implying there is no reason to abstract it. A programmer should understand the bases of what they are programming in (for efficiency). Yes the compiler can handle it. But when it comes to dynamic allocations you end up in the run time. Now it is about perfomance of the app (rather than speed of the build) so not such a good thing. 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.
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:
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.
-
Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address
p
and each item is an offset from there so, for itemn
where each element is sizes
the memory address isp+n*s
. If you want 1 based arrays then the element would be found atp+(n-1)*s
. Which is easier to compute?
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:
The array variable will be a pointer to a memory address
That's just a detail of implementation, this is a conceptual discussion.
-
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
-
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.
-
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.
-
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