views:

143

answers:

6

Once when I was reading some python docs I came across a reference to an article that explained why programming languages with 0-based indexing should always exclude the last element during operations like slicing:

>> a = [1, 2, 3]
>> a[0:1]
[1]  #and not [1,2]

Unfortunately I did not bookmark it. Does anyone know which article I am talking about?

PS: I welcome any explanations of why this is for my immediate satisfaction :-)

A: 

Don't know any specific article, but I think the rationale is simply that this way you get the number of resulting elements via simple subtraction, 1-0=1, instead of having to add 1 there (which you would forget half of the time anyway).

Joonas Pulakka
Thanks but I was really hoping to read the article. For some strange reason I just feel like reading a treatise on this topic!!! o-x
drozzy
+4  A: 

No, but there are at least two good reasons:

  1. a[m:n] gives you n-m elements, making it easy to compute how many elements you are requesting.
  2. With inclusive end-points, it's awkward to request an empty slice (a[3:2]? yuck).

Edit: I just thought of another Python-specific reason: a[m:-n] excludes the first m and last n items. If it was inclusive, it would exclude the first m and last n-1 items, which is much harder to remember.

Marcelo Cantos
Another reason: a[m:n] + a[n:p] = a[m:p]
dF
Good point dF! I forgot about that one.
Marcelo Cantos
+3  A: 

You might be thinking of Dijkstra's short note about zero-based numbering.

Ned Batchelder
Link dead. At least for me.
drozzy
+8  A: 

Could it be this note from E. W. Dijkstra?

just somebody
This could be it! In any case I'll accept it!
drozzy
Just read it - yes that article is great :-) Love the way he derives it by exclusion.
drozzy
+1  A: 

I don't know exactly which article you are referring to, but Googling half-open ranges should find it for you. It found this surprisingly good one that I think is a new personal favorite.

D.Shawley
'Tis a pity that it is not an editable article; the content is good, but the presentation is sloppy with lots of idiosyncratic typos.
Jonathan Leffler
+1  A: 

As far as I know, the first extensive treatment in print was in Koenig's great book, C Traps and Pitfalls -- 20 years old and still in print (indeed, in-stock and shipped immediately from Amazon!-), quite a tribute to its nature as a classic. Unfortunately, there are no previews of it available in Google Books, and while the PDF of the internal report which formed the book is available online, it's obviously much shorter than the book and in particular it does not mention the "open-ranges" issue. There are of course pirate copies on the web, but I don't recommend downloading those.

Several years ago, I summarized Koenig's reasoning here, with a followup discussion here, but of course that's no substitute for the complete treatment as found in his book (though it may be a helpful complement, as in the second post in particular I add other observations in response to critique that was posted on that thread).

Alex Martelli