From where should the index start?
-
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 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
Such low level considerations are why C family uses offsets, indeed. But that isn't particularly relevant for modern high level languages (even something as simple as an array isn't just a pointer in C# or Java, and most 'indexing' in C# or C++ is going through class code anyway). It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.
-
I think you are just considering pointers. Please think about overall problem. In modern programming (languages), importance of pointer is decreasing.
nikunjbhatt84 wrote:
In modern programming (languages), importance of pointer is decreasing.
What is an 'object'? Once you've got to the machine code, it is a lot of data held in memory location. How do you think the machine knows where to find that data? #ping# using pointers! Just because we don't call them pointers in the code does not mean they don't exist. Every object reference compiles down to a memory pointer. You are showing a huge level of ignorance if you really believe pointers are not important. A big mistake being made today is the lack of knowledge of the low level. By studying a bit of machine code you will see very quickly how the computer actually processes a program. Go study the Von Neumann architecture[^] to understand how a computer works. The less memory you use, the faster things can load. The less process steps you use, the faster things can run. Wow! Computing-101 and you don't have a fucking clue why it is important!
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
-
Such low level considerations are why C family uses offsets, indeed. But that isn't particularly relevant for modern high level languages (even something as simple as an array isn't just a pointer in C# or Java, and most 'indexing' in C# or C++ is going through class code anyway). It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.
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
-
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?
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?!?
-
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.
I mostly like loosely typed languages, it decreases programming code. When I program, I know that what type of value the function will accept and return, and I can trap it in anyway by the language's features. So I don't need to know the exact data type.
-
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.
This was my exact thought when I saw he wrote this. When I first started in C# (from C++), this was a feature that I was OOOHHHHH SOOOO HAPPPY with. I thought, wow! How many hours have I spent debugging code that was setting a value to zero when I meant to check the value? [EDIT] This deserves a 1 because why??? Oh and no commment either...
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:
It's pretty difficult to work without lists, and you need to get things out of a list.
Iteration doesn't require indexing to be exposed. If I need to index something by a hard-coded value, then that's bad. If it's a computed value (like a portion of a string) then it should be able to be represented in a better way, again hiding the details of indexing.
BobJanova wrote:
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.
For example? Even matrix operations can be coded with more clarity if the matrix array could be mapped onto a structure so one didn't have to write "[0]". Marc
-
nikunjbhatt84 wrote:
In modern programming (languages), importance of pointer is decreasing.
What is an 'object'? Once you've got to the machine code, it is a lot of data held in memory location. How do you think the machine knows where to find that data? #ping# using pointers! Just because we don't call them pointers in the code does not mean they don't exist. Every object reference compiles down to a memory pointer. You are showing a huge level of ignorance if you really believe pointers are not important. A big mistake being made today is the lack of knowledge of the low level. By studying a bit of machine code you will see very quickly how the computer actually processes a program. Go study the Von Neumann architecture[^] to understand how a computer works. The less memory you use, the faster things can load. The less process steps you use, the faster things can run. Wow! Computing-101 and you don't have a fucking clue why it is important!
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 know that programming languages are highly depending on pointers. You know that what an Object is, but do you know Why the Object is introduced?. Languages are made more and more feature rich to let programmers make solutions by as much less code as possible. What I mean to say by "In modern programming (languages), importance of pointer is decreasing." is, nowadays, programmers do not need to deal with pointers directly, and these increases productivity and actually much higher reliability of code.
-
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:
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.
-
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.
From your post you seem to be implying we should just do without levels of abstraction. So what if the compiler has to do some dirty work for you just because you start at 1 and not at 0? I wouldn't care less, there is no meaningful implication anyhow. Also, the way we count apples, apple nº0 is not meaningful, it is not an apple. Having 0 apples means you have...well no apples at all. We start to count in 1 because that is the first meaningful number when we have apples to begin with, that's why we count all the way up to apple nº10 and do not stop at apple nº9 (apple nº9 is the 10th apple...hmm isnt that obvious? now try explaining that to somenone who has never programmed and you'll really see how intuitive zero based indexing is).
-
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?
I'd make it specifiable so you could decide in each case what made better sense as the base index. (Didn't Algol have that?) I certainly wouldn't mix boolean and integers, though. That's why there are two types!
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
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
The compiler could simply do the -1 calculation, at least for any static indices. You'd have a language where you don't need to do for-loop indexing of arrays if possible (by permitting array operations as a language feature – think APL or R) and anywhere where the looping over an array is done as part of the language, the compiler knows the fixed offsets and can generate the appropriate native code. Only in the case of indexing a fixed-memory array directly by specifying a calculated index would there be an extra operation. And, frankly, that operation is of zero importance for performance on a modern system, where performance is far more about data volumes fitting in chip caches and the parallelisability of algorithms.
-
I know that programming languages are highly depending on pointers. You know that what an Object is, but do you know Why the Object is introduced?. Languages are made more and more feature rich to let programmers make solutions by as much less code as possible. What I mean to say by "In modern programming (languages), importance of pointer is decreasing." is, nowadays, programmers do not need to deal with pointers directly, and these increases productivity and actually much higher reliability of code.
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
-
The compiler could simply do the -1 calculation, at least for any static indices. You'd have a language where you don't need to do for-loop indexing of arrays if possible (by permitting array operations as a language feature – think APL or R) and anywhere where the looping over an array is done as part of the language, the compiler knows the fixed offsets and can generate the appropriate native code. Only in the case of indexing a fixed-memory array directly by specifying a calculated index would there be an extra operation. And, frankly, that operation is of zero importance for performance on a modern system, where performance is far more about data volumes fitting in chip caches and the parallelisability of algorithms.
BobJanova wrote:
The compiler could simply do the -1 calculation
It can't. It would need to place an extra step into the machine code to subtract 1 from 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
-
BobJanova wrote:
It's pretty difficult to work without lists, and you need to get things out of a list.
Iteration doesn't require indexing to be exposed. If I need to index something by a hard-coded value, then that's bad. If it's a computed value (like a portion of a string) then it should be able to be represented in a better way, again hiding the details of indexing.
BobJanova wrote:
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.
For example? Even matrix operations can be coded with more clarity if the matrix array could be mapped onto a structure so one didn't have to write "[0]". Marc
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).
-
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.
-
BobJanova wrote:
The compiler could simply do the -1 calculation
It can't. It would need to place an extra step into the machine code to subtract 1 from 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
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.
-
Clearly, all indices should start from -2 and count in base 13.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Great. Now I'm going to have to find/edit all of my messages where lists start with 0.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
From your post you seem to be implying we should just do without levels of abstraction. So what if the compiler has to do some dirty work for you just because you start at 1 and not at 0? I wouldn't care less, there is no meaningful implication anyhow. Also, the way we count apples, apple nº0 is not meaningful, it is not an apple. Having 0 apples means you have...well no apples at all. We start to count in 1 because that is the first meaningful number when we have apples to begin with, that's why we count all the way up to apple nº10 and do not stop at apple nº9 (apple nº9 is the 10th apple...hmm isnt that obvious? now try explaining that to somenone who has never programmed and you'll really see how intuitive zero based indexing is).
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.