Negative indexing is a well-known feature of Python, for example a[-1]
gets the last element of list a
. Which programming language was the first to do this? (FORTRAN has supported arbitrary indexing for a long time, but -1 is simply another index. This 1965 article proposes that behavior as an extension to FORTRAN II.)
3 Answers
The earliest of which I know is the Icon Programming Language (1977) which allowed negative indexing of lists (one dimensional arrays) and strings as well as slices counting from the end.
An overview of Icon (thanks to qwr) is "The Icon Programming Language: An Overview" and the full reference is "The Icon Programming Language".
P.S. I don't recall if SNOBOL had the same feature.
-
1www2.cs.arizona.edu/icon/ftp/doc/tr78_3.pdf you can add this to your answer– qwrCommented Aug 3 at 20:06
-
2
-
5SNOBOL4 does not implement negative indexes as meaning 'from the end'. And nor could it, since negative indexes are just ordinary indexes in a suitably-declared array.
ARRAY('-10:10')
is a 21-element array with index running from -10 to +19.– daveCommented Aug 3 at 23:20 -
3I couldn't find indexing at all in SNOBOL3 bitsavers.org/pdf/sds/9xx/940/ucbProjectGenie/mcjones/… so safe to say it never had negative indexing.– qwrCommented Aug 4 at 18:29
-
6Uh, in my example of
ARRAY('-10:10')
the upper bound of +19 is a typo, not an indication of utterly bizarre declaration semantics.– daveCommented Aug 5 at 1:47
Negative index is common in newer languages with slices like C#, Julia, Ruby, Perl. Among them Perl is the oldest one, which was first released publicly in 1987
Unfortunately I don't know when the negative index feature was introduced, but it definitely existed at least in Perl 4. I've checked the Perl 1.0 source code from the links below but can't confirm if it supports negative index or not, and I couldn't find Perl 2.0 or 3.0 yet
-
1Ah I was pretty sure but not positive that it was Perl where I first saw it. Commented Aug 3 at 10:14
-
2
-
4However
splice
supported negative indexing from its introduction in 3.0p13 (Mar 1990).– hobbsCommented Aug 3 at 20:19 -
8Technically, C# doesn't use negative indexing like Python does. If you use
-1
as an index, it will error out. C# uses a different syntax for negative indexing,^1
, to avoid situations where the value is computed, rather than a compile-time constant, and the computation erroneously results in a negative number which then gets interpreted as a from-the-end index rather than as an error. Commented Aug 4 at 22:22 -
1@MasonWheeler - thanks for posting that. Though I like and use 'negative indexing' in Python code, it really does seem like an accident waiting to happen.– daveCommented Aug 5 at 23:23
The earliest I can find is the documentation of Mathematica 1.0 (1988).
-
1You may want to give readers a bit more time to add their knowledge. That is beside it making sense to let Tofro write his own answer when he stings it is one.– RaffzahnCommented Aug 3 at 7:17
-
-
@dave Err ... my bad, should read 'thinks' as in "Let Tofro write his own answer when he thinks it is one". (He wrote this 'answer' after Tofro mentioned Mathematica doing this in a comment (now moved to char - another proof why those moves are stupid)) Sorry.– RaffzahnCommented Aug 5 at 22:33
foo[-1]
is perfectly fine given the declarationarray foo[-10:10]
.