开发者

Why are the indexes of Scala tuples 1-based?

"Programming In Scala" explains that tuples'

_N numbers are one-based, instead of zero-based, because starting with 1 is a tradition set by other languages with statically typed tuples such as Haskell and ML

but this can hardly be called an explanation.

Why were tuples defined as 1-based in Haskell and ML in the first place? Are there any mathematical/pr开发者_运维技巧ogramming benefits in having 1-based indexed tuples and not 0-based?

Thanks, Ori


I guess _1, _2 etc it is short for "first", "second", and so on. (fst and snd for instance have historically been used for accessing the left and right part of a tuple). The index in an array on the other hand is an offset and the first element is usually at offset 0.

Are there any mathematical/programming benefits in having 1-based indexed tuples and not 0-based?

No. The elements are not accessed programatically anyway. (You can't do _i if i is an integer.)


Perhaps Haskell and ML used 1-based tuples because COBOL, Algol, and FORTRAN used 1-based arrays. Tradition is a funny thing...


0 based things are great when you want to do math with the index. It just doesn't work with 1 based indexes.

Yet for everybody but computer geeks 1 based is much more natural.

Tuples are intended to contain completely different things. So doing something like add the index of this element and add it to the index of that element and obtain the element with the resulting index don't make sense for tuples.

Therefore I'd say tuples are 1 based, because it is the natural thing to do and there is no reason not to do it.


I guess since tuple is fixed length, defined by user and mostly important is that it's indexed by user, so tuple is 1 based index. Array/List, on the contrary, is indexed by computer (we often index list with list[i] and the i is calculated by Math), so 0 based index is easier.

Just my own guess. Don't know if it's right.


Because starting with 1 is a tradition set by other languages with statically typed tuples, such as Haskell and ML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜