26

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.)

11
  • 3
    I definitely used this feature in some scripting and editing languages in the early 80's and/or late 70's. Commented Aug 3 at 19:22
  • 7
    Negative indexes are just ordinary indexes in languages that are not opinionated about the starting index. In Algol 60, foo[-1] is perfectly fine given the declaration array foo[-10:10].
    – dave
    Commented Aug 3 at 23:23
  • 2
    @dave yes, that's what the whole comment chain was about.
    – qwr
    Commented Aug 4 at 0:25
  • 2
    The comment chain that was moved to oblivion aka 'chat' ? I see no mention there of negative values being legitimate indexes in some languages, only that it was likely an out-of-bound reference (which may or may not be detected).
    – dave
    Commented Aug 4 at 12:49
  • 1
    As a note: supporting negative indices like this takes time for every index to do the conditional check. I expect the pattern is much more common in languages which permit a little extra time on each index (such as for bounds checking).
    – Cort Ammon
    Commented Aug 6 at 4:42

3 Answers 3

29

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.

7
  • 1
    www2.cs.arizona.edu/icon/ftp/doc/tr78_3.pdf you can add this to your answer
    – qwr
    Commented Aug 3 at 20:06
  • 2
    Also I'll look at SNOBOL later
    – qwr
    Commented Aug 3 at 20:11
  • 5
    SNOBOL4 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.
    – dave
    Commented Aug 3 at 23:20
  • 3
    I 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.
    – qwr
    Commented Aug 4 at 18:29
  • 6
    Uh, in my example of ARRAY('-10:10') the upper bound of +19 is a typo, not an indication of utterly bizarre declaration semantics.
    – dave
    Commented Aug 5 at 1:47
18

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

7
  • 1
    Ah I was pretty sure but not positive that it was Perl where I first saw it. Commented Aug 3 at 10:14
  • 2
    AFAICS neither does 4.000 or 4.036; the code here was introduced somewhere in the lost series of patches between 5.000alpha9 (May '94) and 5.000 (Oct '94).
    – hobbs
    Commented Aug 3 at 20:13
  • 4
    However splice supported negative indexing from its introduction in 3.0p13 (Mar 1990).
    – hobbs
    Commented Aug 3 at 20:19
  • 8
    Technically, 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.
    – dave
    Commented Aug 5 at 23:23
5

The earliest I can find is the documentation of Mathematica 1.0 (1988).

3
  • 1
    You 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.
    – Raffzahn
    Commented Aug 3 at 7:17
  • @Raffzahn - 'stings' ?
    – dave
    Commented Aug 5 at 14:46
  • @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.
    – Raffzahn
    Commented Aug 5 at 22:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .