so @ethanhs is new to Mathematica and was wondering why array indexing starts at 1

and the reason behind it is actually pretty weird and due to some interesting language design choices. i thought y'all would enjoy it
Mathematica has a wonderfully quirky functional language that does a lot of things really well. I enjoy using it a lot, though I don't get to use it much these days
so first, some basics. arrays ("lists") in Mathematica are declared with curly braces, and are indexed with double square brackets

they have to be double square brackets because single square brackets are function application (e.g. `Sin[x]`), and parentheses are for grouping
oh, also, Mathematica is symbolic. if it doesn't know what the value of some of your variables (x, y, z here), it's perfectly happy just leaving stuff symbolically

in fact, it won't even numerically evaluate things like square roots until you force it to (with the `N` function)
so the thing is, you *can* index with zero, it just gives you something weird. it says that the zeroeth index of the array is `List`.

what's going on here is that `{5, 6, 7, 8}` is actually sugar for `List[5, 6, 7, 8]`, and the zeroeth index gets you the "head" of an expression
basically, everything in Mathematica is an expression of the form `head[arg, arg, arg, ...]`, or an atom like literals (numbers and strings) or symbols (`x`, `y`, `arr`, etc).

there's sugar for a lot of things, which you can strip away with `FullForm` if curious:
what the indexing operator `[[i]]` actually does is get the i'th index of the expression, with i=0 being the "head" or the name of the "function being called"

this is very similar to how `argv` in C and $n in Bash work: argv[0] and $0 are the name of the program
this means that you can actually apply the indexing operator (which is also sugar for Part https://reference.wolfram.com/language/ref/Part.html) to *any* expression!
so what *is* List, even?

there's a package that lets you poke around function internals. let's use that on Sin, and see that it's defined as a built in kernel function (with some attributes). alright, makes sense
what happens when we call it on List?

it seems like List is defined and has some attributes, but it's not defined *as* anything?
remember that Mathematica is symbolic. it doesn't *need* to know how to evaluate `List`, it's perfectly happy treating it as an opaque name to potentially be pattern matched on later
and that's what's happening here! the index operator ("Part") doesn't care about lists, it cares about expressions! you can make your own list by using a different unbound head:
lists in mathematica aren't some special object. they're just an expression with no semantics on the head, and there's a little bit of syntax sugar for creating them
(this is reminiscent of how lists work in Lisp, for example, though in lisp it is more that "everything is just a list, including expressions" as opposed to Mathematica which is "everything is just an expression, including lists")
so, in conclusion the answer to "why does mathematica index arrays by 1" is "ARRAYS IN MATHEMATICA AREN'T REAL"

~fin~
oh, worth pointing out that matlab is the exact opposite and it really, really, REALLY wants you to use its arrays
and yes, matlab indexes at 1, and it is WRONG
You can follow @ManishEarth.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: