为什么数组从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/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值