[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A modest proposal

It seems to me that there is a perfectly consistent way to have
0-based indexing and negative indices.  Simply allow the arguments
to substring to be arbitrary integers: n denotes the (n mod length)th
character (zero based).  So the third character of "Feap" may be referred
to as any one of ..., -6, -2, 2, 6, ... .  Stripping the last character
off may be done as (SUBSTRING "Feap" 0 -1).

The problem with this is that there is no way to refer to the last
character.  (SUBSTRING "Feap" 3 4) is the empty string, since 4 is
equivalent to 0.  The solution is to number characters mod (+ length 1).
The third character of "Feap" now has indices ..., -8, -3, 2, 7, ... .
This amounts to pretending there's an extra character at the end of every
string, numbered -1.
     (SUBSTRING ... 0 0) is empty
     (SUBSTRING ... 0 -1) is the whole string
     (SUBSTRING ... 0 -2) strips off one character
etc.   Notice that there is no way to get a substring containing the
magic character, so it really isn't there.

A committee should be appointed to study the advisability of having
STRING-LENGTH return the number of characters in the string plus 1.

I never use strings for anything, so you may be sure this proposal
suffers from no untoward biases.