Re: In defense of zero-indexed arrays.

2002-12-09 Thread Mark J. Reed
On 2002-12-06 at 17:59:33, Larry Wall wrote:
 Now all we have to do is
 convince everyone that the year 1 B.C. is the same as year 0 A.D.,
 and 2 B.C. is the same as -1 A.D., and so on.
Well, since that's already true, it hopefully won't take much
convincing.  :)  If you mean to convince the general public to actually
*use* 0 and negative years AD instead of BC, though, that'll take some doing.  
(Astronomers do that already, but they don't count as the general public;
they're more a specific public.) :)

-- 
Mark REED| CNN Internet Technology
1 CNN Center Rm SW0831G  | [EMAIL PROTECTED]
Atlanta, GA 30348  USA   | +1 404 827 4754



Re: In defense of zero-indexed arrays.

2002-12-08 Thread chromatic
On Fri, 06 Dec 2002 14:16:43 +, Brad Hughes wrote:

 In any case, the choice of default base index is less important for Perl than
 for other languages given how seldom arrays in Perl are accessed by index as
 opposed to manipulated by push, pop, for $x (@array) loops and such.

I slice a lot of lists, though, and expect the base index of a loop to
have a certain resemblance to the base index of an array.

-- c



Re: In defense of zero-indexed arrays.

2002-12-06 Thread Brian Ingerson
On 05/12/02 02:45 -0800, Michael G Schwern wrote:
 I'm going to ask something that's probably going to launch off into a long,
 silly thread.  But I'm really curious what the results will be so I'll ask
 it anyway.  Think of it as an experiment.
 
 So here's your essay topic:
 
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
 will benefit most users.  Do not invoke legacy. [1]

With languages like Perl that have negative subscripts, using a zero
base gives continuity. @INC[-2..2] should continue to DWIM.

Cheers, Brian



Re: In defense of zero-indexed arrays.

2002-12-06 Thread Damien Neil
On Thu, Dec 05, 2002 at 02:45:39AM -0800, Michael G Schwern wrote:
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
 will benefit most users.  Do not invoke legacy. [1]

Answer 1: Ignoring legacy, it won't.

Answer 2: Because C uses 0-based indexes, Parrot is written in C, and
it would be just painful to switch back and forth when working on
different layers of the system.  (Not a legacy argument, unless you
want to argue that Parrot is a legacy system.)

Answer 3: In a lower-level language than Perl, an array is usually a
block of memory divided into array elements.  The index is the offset
from the start of the array.  In languages like C which allow pointer
arithmetic, it makes sense for the array index to be the element offset,
to allow a[i] to be equal to *(a + i).  Higher level languages should
follow this convention, for consistency.  (Again, not a legacy argument,
since it offers a first-principles rationale for 0-based arrays in
certain contexts.)

- Damien



Re: In defense of zero-indexed arrays.

2002-12-06 Thread agent . secret
 2002-12-05 10:45:39, Michael G Schwern [EMAIL PROTECTED] wrote:
 I'm going to ask something that's probably going to launch off into a 
 long, silly thread. But I'm really curious what the results will be so 
 I'll ask it anyway. Think of it as an experiment.
 
 So here's your essay topic:
 
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0 
 will benefit most users. Do not invoke legacy. [1]
 
 [1] ie. because that's how most other languages do it or everyone is 
 used to it by now are not valid arguments. Ask any Pascal programmer. 
 :)


The other (reverse) way out, i'm not trying to make an essay, just think 
out loud but if you have $string = Hello World, and you want the last 
three chars, you do:

$wanted = substr $string, -3;

If the first index was 1, it could be ok too, but what would be offset 
0? What if someone was looking at his string backwards?

$pos   = 1; #  0  |  1
# -
substr $string, $pos--, 1;  # 'H' | 'e'
substr $string, $pos--, 1;  # 'd' | 'H'
substr $string, $pos--, 1;  # 'l' | ''  ?
substr $string, $pos--, 1;  # 'r' | 'd' ?


Dont ask me why someone would do that... But i expect to get the last 
$string's char with $pos == -1, not 0.

I also find the 'offset' idea to be consistent with binary math. After 
all, with bytes, 0x7F + 1 == +0d127 but also -0d128... and i found it 
sometimes useful to be able to mix signed and unsigned values.

One could argue it's not the way to go, it's tricky, you dont mix
signed/unsigned... blah. Walking is tricky, bicycling is tricky,
remember the first time you tried and you fell?

blah. no more args :-)
(yet another lurker)






Re: In defense of zero-indexed arrays.

2002-12-06 Thread Brad Hughes
Damien Neil wrote:

On Thu, Dec 05, 2002 at 02:45:39AM -0800, Michael G Schwern wrote:


Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
will benefit most users.  Do not invoke legacy. [1]



Answer 1: Ignoring legacy, it won't.


Bingo.


Answer 2: Because C uses 0-based indexes, Parrot is written in C, and
it would be just painful to switch back and forth when working on
different layers of the system.  (Not a legacy argument, unless you
want to argue that Parrot is a legacy system.)


I doubt most users will be writing Parrot.


Answer 3: In a lower-level language than Perl, an array is usually a
block of memory divided into array elements.  The index is the offset
from the start of the array.


Assuming the base index of the array is 0.  More generally, the index of an
array element is that element's offset from the base index of the array.
Your argument is somewhat circular.  I have oodles of arrays declared to
start at 1980.  Most of my arrays start at index 1.  But then I'm a Fortran
programmer.  (And I hope that's not an opening for a language war thread.)

Choice of language aside, having max_index == num_elements appeals to me. YMMV.

In any case, the choice of default base index is less important for Perl than
for other languages given how seldom arrays in Perl are accessed by index as
opposed to manipulated by push, pop, for $x (@array) loops and such.

brad




Re: In defense of zero-indexed arrays.

2002-12-06 Thread Larry Wall
On Thu, Dec 05, 2002 at 02:45:39AM -0800, Michael G Schwern wrote:
: I'm going to ask something that's probably going to launch off into a long,
: silly thread.  But I'm really curious what the results will be so I'll ask
: it anyway.  Think of it as an experiment.
: 
: So here's your essay topic:
: 
: Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
: will benefit most users.  Do not invoke legacy. [1]

How about, because I like it?  You may, of course, see that as a
legacy argument, depending on our relative ages...  :-)

Anyway, that aside, I see no reason why we couldn't have array types
that are explicitly declared with array bases other than 0.  Perhaps
even the built-in types can just take a range property:

my @array is range(1...);

One could even go so far as to have a pragma that causes all arrays declared
in the current *lexical* scope to be based at 1.  Call it

use fortran;

or some such...

This is not problematical in the same way that $[ was, since we're
limiting the effect to the current lexical scope.  In fact, speaking
of legacy, you'll recall that the fix for Perl 5 was to make

$[ = 1;

really do a lexically scoped declaration despite having the appearance
of a global assignment.

By the way, I noticed when visiting Uruguay that the elevators number
the floors ...-2, -1, 0, 1, 2..., where 0 is the ground floor, and
basement floors are negative.  Way cool.  Now all we have to do is
convince everyone that the year 1 B.C. is the same as year 0 A.D.,
and 2 B.C. is the same as -1 A.D., and so on.

Larry



Re: In defense of zero-indexed arrays.

2002-12-06 Thread Damian Conway
Larry wrote:


: Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
: will benefit most users.  Do not invoke legacy. [1]

How about, because I like it?  You may, of course, see that as a
legacy argument, depending on our relative ages...  :-)


A practical argument in its favour is that it makes circular-lists-via-modulo:

	@list[++nextidx%7] = $nextval;

and cyclic-value-mapping-via-modulo:

	$day_name = Sun Mon Tue Wed Thu Fri Sat[$day%7];

both work correctly.



Anyway, that aside, I see no reason why we couldn't have array types
that are explicitly declared with array bases other than 0.  Perhaps
even the built-in types can just take a range property:

my @array is range(1...);


Surely, that would be:

  my @array is domain(1...);

???

Damian




Re: In defense of zero-indexed arrays.

2002-12-06 Thread Uri Guttman
 DC == Damian Conway [EMAIL PROTECTED] writes:

  DC A practical argument in its favour is that it makes
  DC circular-lists-via-modulo:

  DC   @list[++nextidx%7] = $nextval;

  DC   $day_name = Sun Mon Tue Wed Thu Fri Sat[$day%7];

  DC both work correctly.

not to defend 1 based arrays but all you have to do with the above is
add the base offset to them:

  DC   @list[++nextidx%7 + 1] = $nextval;
  DC   $day_name = Sun Mon Tue Wed Thu Fri Sat[$day%7 + 1];

in any case i like 0 based. the best argument i have seen so far is that
is makes -1 a meaningful index. that is something pl1 and fortran could
never do without a range declaration.

and larry's property and pragma ideas are fine solutions for those who
want impaired indexing. :)

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
- Stem and Perl Development, Systems Architecture, Design and Coding 
Search or Offer Perl Jobs    http://jobs.perl.org



Re: In defense of zero-indexed arrays.

2002-12-05 Thread Luke Palmer
 Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
 Date: Thu, 5 Dec 2002 02:45:39 -0800
 From: Michael G Schwern [EMAIL PROTECTED]
 Content-Disposition: inline
 Sender: Michael G Schwern [EMAIL PROTECTED]
 X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/

 I'm going to ask something that's probably going to launch off into a long,
 silly thread.  But I'm really curious what the results will be so I'll ask
 it anyway.  Think of it as an experiment.

 So here's your essay topic:

 Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
 will benefit most users.  Do not invoke legacy. [1]

Through years of experience:  Because it's cleaner that way.

from 1:A   Z
   ↓   ↓
+--+--+--+--+--+
  $x:   | 1  | 2  | 3  | 4  | 5  |
|  |  |  |  |  |
+--+--+--+--+--+
↑  ↑↑  ↑
from 0: a  by  z

They're just different ways of thinking.  If you start from 1, you're
talking about the elements themselves; operations are [i,j]
(inclusive).  If you start from 0, you're talking about the positions
between elements; operations are [i,j) (inclusive, exclusive).

Say you have $x as above, and you wish to partition it into two
strings 12 and 345.  In the 1 paradigm:

$part  = 3;
$first = substr $x, 1, $part-1;
$last  = substr $x, $part, 5;

In the 0:

$part  = 2;
$first = substr $x, 0, $part;
$last  = substr $x, $part, 5;

In the former, you can call $part 2 if you want; it's equally as ugly.
I'm having flashbacks to my QBASIC days, where anything that
manipulated arrays seemed to be flooded with +1 and -1 in that way.
They say C has off by one errors, they have not tried BASIC.

I know this wasn't a strong argument, but in summary, most algorithms
are more elegant when working with spaces between elements than with
the indices of the elements themselves.  And it only makes sense to
number them from zero then (otherwise you get length+1 as the end,
which doesn't make any sense). 

Luke




Re: In defense of zero-indexed arrays.

2002-12-05 Thread Richard Proctor
On Thu 05 Dec, Michael G Schwern wrote:
 So here's your essay topic:
 
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start at 0
 will benefit most users.  Do not invoke legacy. [1]

 [1] ie. because that's how most other languages do it or everyone is
 used to it by now are not valid arguments.  Ask any Pascal programmer. :)

Many years ago I was involved with a project where all the software
people reffered to the hardware as planes 0 and 1 (it was a duplicated 
system) and the hardware people always used 1 and 2.  To avoid confusion
we settled on using 0 and 2.

Any way of indexing arrays has its proponents.  Perl currently has the
heavily depreciated $[ to allow playing with this base, changing it has 
nasty affects at a distance.

Long long ago some computer languages did base their arrays at 1 rather
than 0.  Hopefully they are dead now - it led to confusion and bad practices.
But that is a legacy argument.

There was an argument when computer languages were close to the hardware,
when to index an array you added the index (multiplied by the size of
the element) to the base of the array to find what you wanted.  This is
probably insignificant and not an issue today.

To conclude other than a very large legacy argument, there is probably
no strong reason to base arrays at 0 rather than 1.  I would not want to
change.

Richard


-- 
Personal [EMAIL PROTECTED]http://www.waveney.org
Telecoms [EMAIL PROTECTED]  http://www.WaveneyConsulting.com
Web services [EMAIL PROTECTED]http://www.wavwebs.com
Independent Telecomms Specialist, ATM expert, Web Analyst  Services




Re: In defense of zero-indexed arrays.

2002-12-05 Thread Austin Hastings
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start
 at 0 will benefit most users.

The languages which do not start their indices at 0 are dead or dying. 

 Do not invoke legacy. 

How about FUD? :-)

=Austin

--- Michael G Schwern [EMAIL PROTECTED] wrote:
 I'm going to ask something that's probably going to launch off into a
 long,
 silly thread.  But I'm really curious what the results will be so
 I'll ask
 it anyway.  Think of it as an experiment.
 
 So here's your essay topic:
 
 Explain how having indexes (arrays, substr, etc...) in Perl 6 start
 at 0
 will benefit most users.  Do not invoke legacy. [1]
 
 
 [1] ie. because that's how most other languages do it or everyone
 is used
 to it by now are not valid arguments.  Ask any Pascal programmer. :)