In most programming languages, container indices start at 0. This is not random or hardware-related; for example Dijkstra's article explains why zero-based indices make sense.
What are the reasons why Mathematica lists start with an index of 1?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIn most programming languages, container indices start at 0. This is not random or hardware-related; for example Dijkstra's article explains why zero-based indices make sense.
What are the reasons why Mathematica lists start with an index of 1?
I think Leonid's answer deserves to be expanded upon. Most other languages are not symbolic, and thus the "variable name" is not something one needs to keep track of --- ultimately the interpreted or compiled code is keeping track of pointers or something. In contrast, in Mathematica the Head
of an expression is arbitrary. This is somewhat along the lines of LISP where the first symbol in a list is the procedure which should be applied to the rest of the list. So, in LISP one might write (+ 3 2)
which evaluates to 5
. Written this way, it's clear that the symbol +
occupies the "natural" 0th position, 3
the first, and 2
the second. In Mathematica one would write the equivalent expression as Plus[3,2]
, so that the 3
is in the first position -- the same position that it would be in in LISP. The fact that some Head
s (namely, List) work like vectors for many intents and purposes would break the uniformity of the mapping between a LISP-like language and Mathematica, and worse---break the internal uniformity of Mathematica indexing, if you demand that you should be able to extract the Head
of an expression.
This is related to the fact that in some sense, it's the most symmetric thing to do in a symbolic language, if that language is going to support negative indexing and arbitrary Head
s. For example if you have
f = F[1,2,3,4,5]
then f[[-1]] evaluates to 5
. If you impose "periodic boundary conditions" you might imagine writing the expression f
schematically as
F
5 1
4 2
3
so that moving one spot clockwise gives you the first element, one spot counterclockwise gives you the last element, and moving 0 spots gives you the Head
.
Slot[]
; #0
refers to the function itself (useful for recursive implementations).
$\endgroup$
Dec 22, 2016 at 3:46
Some years ago, a friend of mine was in the supermarket with his son, small kid, who asked him to buy some candy. After some resistance, my friend agreed, but told him that should be just one. In the cashier, his son had two candy, and my friend said:
And the answer (very smart) was:
Well... I believe starting to count lists of discrete items by 1 is much more natural. I don't understand why other languages do it starting by Zero, if it's not a continuum interval.
Mathematica, Matlab and R are the ones I know that follows this convention.
p
, then p[0]
is the same as *(p+0)
and the same as *p
and the same as 0[p]
.
$\endgroup$
lst = {1,2,3}; lst[[0]]
, which in this case isList
. $\endgroup$1<=i<=N
seems to be a quite natural choice for ranges to me... $\endgroup$