Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the indexes of Scala tuples 1-based?

Tags:

scala

"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/programming benefits in having 1-based indexed tuples and not 0-based?

Thanks, Ori

like image 865
Ori Avatar asked Jun 05 '11 07:06

Ori


People also ask

Is Scala zero indexed?

Let's first define an array in scala. Then you will modify a value at the 1st index since scala arrays are zero-indexed and see if you can update it.

Are tuples zero indexed?

Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. tup1 = (50,); Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.

What is a benefit of using tuples in Scala?

In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable. Tuples are especially handy for returning multiple values from a method.

What is indexing in Scala?

it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one. It is a collection of mutable values.


1 Answers

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.)

like image 133
aioobe Avatar answered Oct 12 '22 01:10

aioobe