Habit is habit,
and not to be flung out of the window by
any man, but
coaxed downstairs a step at a time.
— Pudd'nhead
Wilson's Calendar
Откуда вообще
началось использование 0-based индексации?
Есть мнение,
что начало было положено создателем
языка BCPL, который делал язык, приближенный
к машинному коду. Где регистры и машинные
слова содержат числа, представляющие
что угодно. Где адресная арифметика
логично начинается с 0:
The fact of it is
this: before pointers, structs, C and Unix existed, at a time when
other languages with a lot of resources and (by the standard of the
day) user populations behind them were one- or arbitrarily-indexed,
somebody decided that the right thing was for arrays to start at
zero.
…
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.
Что интереснее,
автор находит у этого решения социальный
контекст, не очень лестный для руководства
IBM. А в конце статьи сокрушается о том,
что люди всегда выбирают краткосрочную
перспективу в ущерб долгосрочной, после
чего отказываются от ответственности
за свои решения, говоря «это было
неизбежно».
Познавательно.
А знаете, почему
в Python индексация zero-based? Гвидо решил, что
в таком случае слайсы выглядят изящнее:
Using 0-based
indexing, half-open intervals, and suitable defaults (as Python ended
up having), they are beautiful: a[:n] and a[i:i+n]; the former is
long for a[0:n].
Using 1-based
indexing, if you want a[:n] to mean the first n elements, you either
have to use closed intervals or you can use a slice notation that
uses start and length as the slice parameters. Using half-open
intervals just isn't very elegant when combined with 1-based
indexing. Using closed intervals, you'd have to write a[i:i+n-1] for
the n items starting at i. So perhaps using the slice length would be
more elegant with 1-based indexing? Then you could write a[i:n]. And
this is in fact what ABC did -- it used a different notation so you
could write a@i|n.(See
http://homepages.cwi.nl/~steven/abc/qr.html#EXPRESSIONS.)
But how does the
index:length convention work out for other use cases? TBH this is
where my memory gets fuzzy, but I think I was swayed by the elegance
of half-open intervals. Especially the invariant that when two slices
are adjacent, the first slice's end index is the second slice's start
index is just too beautiful to ignore. For example, suppose you split
a string into three parts at indices i and j -- the parts would be
a[:i], a[i:j], and a[j:].
So that's why Python
uses 0-based indexing.
Такие дела.
original post http://vasnake.blogspot.com/2013/10/zero-based-indexing.html
Комментариев нет:
Отправить комментарий