1-based arrays

Monkey Forums/Monkey Programming/1-based arrays

c.k.(Posted 2011) [#1]
Would it be too much to ask for 1-based arrays. I program in other languages that use it and coming back to a 0-based array is rather disconcerting because it seems so idiotic.

What's the first element? The zeroeth one! WTF?!

We are humans. Computers bend to our will, not the other way around.

0-based is not logical, and Spock agrees.


Dima(Posted 2011) [#2]
http://en.wikipedia.org/wiki/Zero-based_numbering

Array indices start at 0 for almost all languages.

The reason is that historically the index of an array indicates its offset from a certain address in memory.

Lets say you have an array of 4-byte integers. The array starts at memory address 0x1000. The element w/ index 0 is at 0x1000. That with index 1 is at 0x1004. Index 2 at 0x1008. And the nth is at 0x(1000+4*n).

You probably wouldn't notice this in C++ or C#, but C-style arrays bring this to the forefront in the way that *A (address of A) indicates the beginning of array A and pointer arithmetic can bring you to later elements.

http://www.augustcouncil.com/~tgibson/tutorial/arr.html

"Why do arrays in C and C++ start at zero instead of one?". We know that the name of the array produces the address of the beginning of the array; and we know that the beginning of the array is also the address of the first element of the array. So, all of these are exactly the same:

*age
*(age+0)
age[0]

The bottom line: Arrays are nothing more than a bunch of variables of the same type using the same name--that name is the address of the beginning of the bunch of variables.


muddy_shoes(Posted 2011) [#3]
Not logical? Dijkstra disagrees: http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html.


Jesse(Posted 2011) [#4]
please don't. it's just fine the way it is.

I would hate to have to readopt to that mundane way of coding. :)


c.k.(Posted 2011) [#5]
LOL... muddy, I saw that page as I was researching the concepts.

Dijkstra is wrong about every other thing. :P

I understand WHY we do zero-based arrays, but it's no longer valid.

a[] represents an array of elements, the first of which is #1, not #0. It's no longer about memory locations for the programmer, it's about the position in a sequence of elements.

sheesh.


Dima(Posted 2011) [#6]
Most programmers will expect first index to be 0 (historically), why force new methodology on the majority when they are comfortable already?


maverick69(Posted 2011) [#7]

0-based is not logical, and Spock agrees.



Don't think Spock would agree! He's on Dimas side ;)

BTW: Which languages support 1-based array indexes?


c.k.(Posted 2011) [#8]
Because we don't ride horses anymore to go from A to B. We use cars. That is, updates can be good. :)

It is more logical to call the first element in an array [1], not [0].

I know it's not going to change for Monkey, but don't try to convince me that it's logical. It's a holdover from early languages. Today's high-level languages don't need it.


Dima(Posted 2011) [#9]
It's more like electric cars vs traditional, gets you from point A to B but needs alternate way of refueling, which isn't supported by every gas station (yet).


MikeHart(Posted 2011) [#10]
+1 for zero-based arrays


matty(Posted 2011) [#11]
No preference either way for myself, but I can also see the logic behind both methods


therevills(Posted 2011) [#12]
Monkey has zero based arrays - End of Story :P

Its been out now for a few months, imagine all the code it would break if it went to 1 based arrays.


Redbeer(Posted 2011) [#13]
It has historically been zero.
The number line is referenced to zero.
Cartesian coordinates reference to zero.
Every time I do any problem, I generally look for things that are zero or can be chosen as zero.
I vote for it to stay zero based. :D


jalski(Posted 2011) [#14]
Zero based arrays are fine.

But why not give programmer a choice?


I myself like the PL/I style arrays:

For an example:

To declare a two dimensional 8x8 board integer array starting from index 0:

dcl board(0:7, 0:7) fixed bin (31);

Say, you want to store some average temperature information for years 2000 - 2011. Why not just declare array like this:

temperature(2000:2011, 365)


c.k.(Posted 2011) [#15]
VBA has an "Option Base n" statement where you can specify the array base. That would be nice for those of us who prefer logic over tradition. :)


AdamRedwoods(Posted 2011) [#16]
A 0 based array can be made into a 1 based array with a little extra coding.

i[0] = null or -1
i[1] = values start here


marksibly(Posted 2011) [#17]
Hi,

I think there are arguments to be made either way - depends on the context.

In a math/graphics context, 0 based is useful for 'bucket' style arrays - eg: tile maps etc.

For a text processing or other 'counting' related app, perhaps 1 would be better, eg: line 1,2,3...word 1,2,3...dice-roll 1,2,3 etc..

However, Monkey is 0 based because all it's target languages are, so it's really just the most convenient/efficient way to do it.

If a lua translator or something happened that needed 1 based arrays I'd have to add some hacks - but I'd rather be doing that for the minority of languages.

Adding a 'one based arrays' option would not really be feasible without slowing down ALL arrays, as arrays would then have to carry their 'base' around with them.


Samah(Posted 2011) [#18]
If a lua translator or something happened that needed 1 based arrays I'd have to add some hacks - but I'd rather be doing that for the minority of languages.

Lua does not have indexed "arrays" as such. Tables are key/value pairs, so your index can be whatever you want. The only real gotchas are that using table.insert() on an empty table defaults to 1, and the ipairs() iterator function indexes from 1. You can still index it however you want.
mytable = {}
mytable[0] = 'a'
mytable[1] = 'b'
for k,v in pairs(mytable) do -- k is the key, which will be whatever indices you used
for i,v in ipairs(mytable) do -- i is the ordered index of the key, which is 1-based
for i = 0,9 do -- just index your table with i



JaviCervera(Posted 2011) [#19]
I also think that zero-based arrays are fine. I am used to them on every language that I use.