爲什麼數組從0開始

今天看到一篇文章,《Python語言的創始人解釋爲什麼Python數組的索引從0開始》http://www.aqee.net/why-python-uses-0-based-indexing/.


他給的理由是:

"使用0-based的索引方式,Python的半開區間切片和缺省匹配區間切片語法變得非常漂亮:a[:n] 和 a[i:i+n],前者的標準寫法就是a[0:n]。"

a[:n]表示取從0到n-1這n個元素.即是[ )這樣一個前閉合,後開的半開區間.

a[i:i+n]表示取從a[i]到a[i+n-1]這n個元素.而對於下標從1開始的數組就不能這麼簡單的表示.在使用半閉合切片方式時,如果用1開始的,就會表示成a[i,i+n-1]


你想將一個數組以i,j兩個點切成三部分——這三部分將會是a[:i]a[i:j]a[j:]

a[:i]表示取了前i個元素,

a[i:j]表示取了從a[i]開始到a[j-1]結束。共j-i個元素

a[j:]表示取從a[j]開始到最後一個元素。


若用基於1的半開區間表示:

a[1,i+1]

a[i+1,j+1]

a[j+1,最後]

若用基於1的閉區間表示:

a[1,i]

a[i+1,j]

a[j+1,最後]


可見都沒有使用基於0的半開區間方便。


查了一下,有一篇http://exple.tive.org/blarg/2013/10/22/citation-needed/說是決定使用基於0的數組的創始人http://www.cl.cam.ac.uk/~mr10/給的答案是:


As for BCPL and C subscripts starting at zero. BCPL was essentially designed as typeless language close to machine code. Just as in machine code registers are typically all the same size and contain values that represent almost anything, such as integers, machine addresses, truth values, characters, etc. BCPL has typeless variables just like machine registers capable of representing anything. If a BCPL variable represents a pointer, it points to one or more consecutive words of memory. These words are the same size as BCPL variables. Just as machine code allows address arithmetic so does BCPL, so if p is a pointer p+1 is a pointer to the next word after the one p points to. Naturally p+0 has the same value as p. The monodic indirection operator ! takes a pointer as it’s argument and returns the contents of the word pointed to. If v is a pointer !(v+I) will access the word pointed to by v+I. As I varies from zero upwards we access consecutive locations starting at the one pointed to by v when I is zero. The dyadic version of ! is defined so that v!i = !(v+I). v!i behaves like a subscripted expression with v being a one dimensional array and I being an integer subscript. It is entirely natural for the first element of the array to have subscript zero. C copied BCPL’s approach using * for monodic ! and [ ] for array subscription. Note that, in BCPL v!5 = !(v+5) = !(5+v) = 5!v. The same happens in C, v[5] = 5[v]. I can see no sensible reason why the first element of a BCPL array should have subscript one. Note that 5!v is rather like a field selector accessing a field in a structure pointed to by v.


翻譯:

對於BCPLC的下標從0開始.BCPL本質上被設計成無類型的語言,類似於machine code.就像machinecode中的寄存器,它們都擁有相同的大小,存儲的內容可以表示爲任何東西,比如整型,機器地址,truth values,字符等等.

BCPL擁有無類型變量,就像機器中可以存儲表示任何東西的寄存器一樣.

 

如果BCPL變量表示一個指針,它就指向一個或者多個連續的內存塊,每塊大小都是一樣的.正如機器碼,BCPL也允許地址的計算..如果p是一個指針,那麼p+1就指向緊接着p所指向的那個內存塊後面一個的地方.

自然,p+0p指示的是同一個位置.

沒有必要讓p+1表示該數組中的第一個元素.

使用間接運算符"!"取得所指位置中的內容.如果v是一個指針,那麼"!(v+1)"就表示v所指內存後面那塊中的內容.

 

隨着i0開始增大,我們就可以訪問從v指向的內存開始的連續的空間中的內容.

 

"!"也可以這麼用: v!i,同樣可以表示 !(v+i).

非常自然的想到,在一個數組中,第一個元素的下標從0開始.

 

C語言中的"*"作用和BCPL中的"!"一樣,間接運算符.[]作爲數組下標.


Note :

BCPL v!5 = !(v+5) =!(5+v) = 5!v

C語言中 a[1] = 1[a]


此外,還有一些理由說是因爲從0起可以節省編譯時間.

http://exple.tive.org/blarg/2013/10/22/citation-needed/


發佈了38 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章