Some questions on Slate syntax

David Hopwood david.nospam.hopwood at blueyonder.co.uk
Sun Mar 27 07:08:21 PST 2005


Shaping wrote:
> Lee Salzman wrote:
>> The whole "0-based" vs. "1-based" thing is misleading. When you are 
>> talking about "at:", you are locating some item in space by a 
>> coordinate - addressing. Memory is a spatial thing. Counting is just a 
>> different idiom entirely - searching. The thinking is, "I want that 
>> thing, there!", as opposed to, "Hmm, that's not what I want. Not that 
>> either. Oh, this one!"
> 
> Discrete, /ordered/ items are countable.  Counting starts at 1.
> 
> Fundamentally, this issue is about naming a thing with a number 
> representing its /place/ in an ordered sequence.

Yes, I agree that spatial metaphors are most appropriate for arrays
and indexing. Think of the index as corresponding to a distance from
the start of the sequence, and as pointing to the left [*] of the item
being indexed:

   0   1   2   3   4
   +---+---+---+---+
   |   |   |XXX|   |
   +---+---+---+---+

   <---2--->


For intervals, use the half-open convention and think of the bounds
as being the left and right sides of the bounded interval, e.g. for
the interval [1,3):

   0   1   2   3   4
   +---+---+---+---+
   |   |XXX|XXX|   |
   +---+---+---+---+

   <-1->
   <-----3----->


[*] for people used to left-to-right languages.

> Order and counting both start at 1.

Natural numbers start at 0.

Bill Sun wrote:
 > Furthermore, when counting, of course it would start at the first
 > natural number.

which is 0. You're probably thinking of "ordinal numbers".

 > With arrays, we often find ourselves iterating over them.  So it
 > seems natural to refer to the n-th element in the array as...well,
 > the n-th element.

There's nothing "natural" about either way (the technical meaning
of "natural number" notwithstanding); they're just conventions.
If you're used to the 0-based convention, then you think of the element
at 0-based index n as being element n, and don't think about the
"n-th element" at all.

 > I guess this is why Smalltalk took the 1-based array approach.  As
 > it is designed to be a high-level language that is more natural for
 > humans to interact with.  I believe when C adopted 0-based arrays,
 > it was because it was faster, as memory locations starts from 0.

C just followed the convention that had been established by Algol,
Pascal, and assembly languages. (0-based indexing is also simpler in
languages that support pointer arithmetic.)

I don't think this has much to do with performance: Fortran, which has
a stronger performance emphasis than C, uses 1-based indexing. Also
it's possible to eliminate almost all of the performance difference in
the language implementation by subtracting one element from the base
pointer for each array.

-- 
David Hopwood <david.nospam.hopwood at blueyonder.co.uk>




More information about the Slate mailing list