Arc Forumnew | comments | leaders | submitlogin
Negative indexes for strings
3 points by Qu4Z 5112 days ago | 8 comments
I think it would be neat for negative indexes to strings (and lists, and other types like this) to start from the right (or be relative to the length) as is done in, for example, python. This would be more concise than writing a (from-right "testing" 1) method, and presumably wouldn't interfere with any other features.

PS: First time posting (and registering). I've been lurking for a while, but if I've made any HN/ArcLangForum faux pas...s, please let me know)



1 point by evanrmurphy 5112 days ago | link

I like this idea and think it fits well with Arc's theme of compactness.

What are all the possible implications? For example, it could be implemented as an isolated feature, but having negative indexes start from the right kinda makes me crave a '-car and '-cdr that start from the right as well. Additionally, if you're going to wrap around at the zero index, does it make sense to wrap around at all indices where mod length equals zero? That is, does

  (let xs '(a b c)
    (and (is xs.0 xs.3)
         (is xs.1 xs.4)
         (is xs.2 xs.5))  ; etc.
evaluate to true? This would be kind of a serious change and probably not very useful, IMO. (To be sure, I'm not really suggesting that these follow from your idea per se but rather from taking its characteristics to extremes.)

Finally, just a ssyntax question: would

  xs.-1
work? I think it certainly should if negative indexing were implemented, just wondering if our underlying machinery might have trouble correctly parsing the dot and hyphen together.

-----

2 points by fallintothis 5112 days ago | link

  arc> (ssexpand 'xs.-1)
  (xs -1)
To model indexing after Python, valid indices for a sequence of length n would be integers in the range [-n, n).

  >>> xs = ['a', 'b', 'c']
  >>> xs[0] is xs[3]
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  IndexError: list index out of range
  >>> xs[0] is xs[-6]
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  IndexError: list index out of range
  >>> xs[0] is xs[-3]
  True

-----

1 point by evanrmurphy 5112 days ago | link

Glad the 'ssexpand works, I should have tried that.

Python's indexing doesn't wrap around infinitely, but that doesn't mean Arc's couldn't.

-----

1 point by fallintothis 5112 days ago | link

Hence "To model indexing after Python". I figure Python's probably a good source of inspiration for Python-like features. ;)

Admittedly, I can't find specific rationale for IndexErrors discussed anywhere. Perhaps throwing errors for egregious indices just seems the "sane" thing to do. Essentially, it's a choice of foist: if indexing wraps around infinitely, you need to check bounds when you don't want to wrap around; if indexing throws errors, you need to mod explicitly when you do want to wrap around.

-----

1 point by evanrmurphy 5112 days ago | link

> if indexing wraps around infinitely, you need to check bounds when you don't want to wrap around; if indexing throws errors, you need to mod explicitly when you do want to wrap around.

Good summary. I agree that index-out-of-bound errors are the saner default.

-----

1 point by akkartik 5112 days ago | link

Ah, I knew this had come up recently: http://arclanguage.org/item?id=10880 via http://arcforum.searchyc.com/negative+index

:) Not quite everything you want, but I think cut covers the biggest use case.

-----

3 points by Qu4Z 5112 days ago | link

Thank you. That's pretty neat, and I'm sure it'll be useful.

However, I feel it is addressing a slightly different point from my post. I can easily write my own library function to do, eg

  (from-right '(1 2 3 4 5) 1)
I do feel the succinctness and power of the language would be enhanced by including the ability to instead call

  ('(1 2 3 4 5) -1)
Of course, other people may have a differing opinion.

Implementation is rather simple... you could essentially take all indexes mod the length of the collection. Although perhaps it just doesn't help that much outside of Project Euler :-)

-----

1 point by akkartik 5112 days ago | link

No our opinions are in accord.

-----