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?
Your example is flawed: zero indexing can return "string not found inside another string" - and does - by returning a negative index. Why multiply methods if it is not necessary?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
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 start at zero. And have you tried Pascal?
-
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?
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) -
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)Did you have something to do with the development of APL?:~
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
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)Clearly ;P
-
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?
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.
-
Did you have something to do with the development of APL?:~
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
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
-
Now be fair, you can't set the index origin to -2 in APL. I think you're after VB or Pascal/Delphi here (both allow you to index arrays arbitrarily).
I wouldn't be so sure, those characters are pretty impenetrable to mere mortals like me... :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
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?
Indices start from 1. (When did you ever hear someone talking about the 'zeroth' anything, outside of certain computing circles?) Offsets start from 0. The confusion comes because the C family really uses offsets (you can see this in the original C, with the identity a[i] = a + i), but they've always been called 'indices', and that has fed forward into '0 based indexing' (which means subscripts are offsets, really) in its modern siblings. Once you know whether you want to base your language on indices or offsets, the mechanics become obvious. Index: start from 1. ("What's the first/second/etc character of a string?" -> "s[1/2/etc]".) Offsets start from 0. ("What's the character 0/1/etc away from the start?" -> "s[0/1/etc]".) Since I come from the real world and not a computer science course I naturally think in terms of index origin 1 – particularly as I started out programming in APL with that setting. I've written conventional languages enough that I can deal with the offset-based approach but if I was choosing I'd still start at 1.
-
I wouldn't be so sure, those characters are pretty impenetrable to mere mortals like me... :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
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?
nikunjbhatt84 wrote:
Then from where would start the index number?
I wouldn't allow indexing. At least, I wouldn't expose it to the programming language. Indexing is an archaic left-over from the days for for-next loops and arrays. Even for strings, it's absurd. Folks should take a page from assembly language, where you could map memory into a structure and reference specific elements of the structure with meaningful symbols. This includes handling strings dynamically as well, if people were just to put there minds to it. 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
I think you are just considering pointers. Please think about overall problem. In modern programming (languages), importance of pointer is decreasing.
-
nikunjbhatt84 wrote:
Then from where would start the index number?
I wouldn't allow indexing. At least, I wouldn't expose it to the programming language. Indexing is an archaic left-over from the days for for-next loops and arrays. Even for strings, it's absurd. Folks should take a page from assembly language, where you could map memory into a structure and reference specific elements of the structure with meaningful symbols. This includes handling strings dynamically as well, if people were just to put there minds to it. Marc
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.
-
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.