> 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      b                    y      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

Reply via email to