[REBOL] Re: On ordinal and cardinal numbers...
From: joel:neely:fedex at: 7-Jul-2001 15:56
Jeff Kreis wrote:
> Okay, but the important caveat for everyone to remember is
> that with 1-based indexing, LENGTH? blk corresponds to the
> range of positions that can be picked from, changed, etc.
> However, with zero-based indexing, LENGTH? blk refers to 1+
> the range of values pickable, changable.
>
No offense, but if I didn't already know what we were talking
about, I'd have lots of trouble parsing that description.
How about saying it this way:
One-origin indices range from 1 to LENGTH? blk.
Zero-origin indices range from 0 to (LENGTH? blk) - 1.
This really is familiar to anyone who's used 0-origin. The
60 minutes in an hour range from 0 to 59. The 24 hours in
a standard 24-hour clock range from 0 to 23.
> So, for example, the following code behaves
> differently depending on the base:
>
> x: [1 2 3]
> change at x length? x 0
>
> 1-based == [1 2 0]
> 0-based == [1 2 3 0]
>
We're not talking about changing the behavior of AT, but
whether we might have a *new* word ATZ (or AT0 or AT/ZERO).
Therefore anyone who wanted to change the last element of X
could write any of the following
change at x length? x 0
change atz x (length? x) - 1 0
change back tail foo
> This leads to a change of REPEAT as part of a very common
> REBOL idiom:
>
> repeat i length? block [ print [i pick block i ] ]
>
> Works for 1-based, but without changing both REPEAT and
> LENGTH? will break for 0-based (skips the zeroth element and
> prints NONE on the final iteration).
>
The above fragment of code would still work. Anyone who knew
(or had learned) enough to write the 0-origin versions could
also write it as
repeat i length? block [print [i - 1 pickz block i - 1]]
Unless, of course, you're joining with me to extend the list
of suggested new words to include REPEATZ, so that one could
write it as
repeatz i length? block [print [i pickz block i]]
The only rule is "Be consistent." Don't mix origins unless
you know what you're doing.
-jn-
---------------------------------------------------------------
There are two types of science: physics and stamp collecting!
-- Sir Arthur Eddington
joel-dot-neely-at-fedex-dot-com