[comp.lang.icon] Substring positions

lied@ihuxy.ATT.COM (Bob Lied) (12/25/87)

I've finally gotten enough spare time to take a
look at ICON, and I have a novice question (please
don't hit me).  In an example and a solution in
chapter 5 of the book (I assume you know the book
I refer to), the size of a substring is calculated
with
	*line[i:j]
where I would intuitively expect to use
	i-j

I am inferring from this that string positions are
like other languages' pointers in that their numerical
values are not to be trusted, and I should limit their
use to assignments, comparisons, and defined operations
like subscripting.  Am I right?

	Bob Lied	ihnp4!ihuxy!lied
	AT&T Bell Laboratories

gudeman@ARIZONA.EDU ("David Gudeman") (12/31/87)

   From: ihnp4!ihuxy!lied@ucbvax.Berkeley.EDU  (Bob Lied)

   ....  In an example and a solution in chapter 5 of the book (I
   assume you know the book I refer to), the size of a substring is
   calculated with

	   *line[i:j]
   where I would intuitively expect to use
	   i-j

   I am inferring from this that string positions are
   like other languages' pointers in that their numerical
   values are not to be trusted, and I should limit their
   use to assignments, comparisons, and defined operations
   like subscripting.  Am I right?

Well, not really.  String positions are integer positions _between_
the characters of the string, where 1 is the position at the beginning
of the string.  However, non-positive integers are also valid string
positions (with 0 at the end of the string).  So if i or j is
non-positive, it is not true that "*line[i:j] = j-i" (I assume you
meant "j-i" rather than "i-j").

In the example in the book, both i and j are guaranteed to be
positive, so "j-i" works.  Notice though, that in most languages the
positive integers refer to character positions, so the correct formula
for the length of a substring from i to j is "j-i+1".  Perhaps the
authors wanted to avoid confusing readers with this subtlety.

shafto@AMES-AURORA.ARPA (Michael Shafto) (01/01/88)

For what it's worth:  When I first started using Icon, I thought
the string/list subscripting conventions were extremely
counter-intuitive and confusing; and, in fact, they caused me
to make a number of annoying errors.  E.g., even if you know
better, a := el[1:3] looks like a good way to pick off the
first three elements of a list.

Now it seems to me that these conventions are unusual, but
worth getting used to.  Once you get the hang of them, they
seem pretty neat.

Mike