Here I am again with an annoying question :-)
First a nice quote.
This is all according to documentation, so far so good. I am not using implicit indexing here, since I want to illustrate my question with a clear example.peter[~]$ newlisp
newLISP v.9.2.11 on Linux, execute 'newlisp -h' for more info.
> (set 'example '("a" "b" "c" "d" "e"))
("a" "b" "c" "d" "e")
>
> (length example)
5
> (nth 4 example)
"e"
> (nth 5 example)
"e"
> (nth 6 example)
"e"
> (nth 0 example)
"a"
> (nth -1 example)
"e"
> (nth -2 example)
"d"
My question relates to the logic of indexing. If the index grows, and gets larger than the the amount of elements, the last element is returned. No matter what number higher than the amount of elements, the last element is returned.
However, if the index gets smaller, even smaller than 0, not the first element is returned, but the indexing wraps around and all of a sudden returns the last element of the list again.
In my point of view it would be more consequent to return the first element of a list if the index gets smaller than 0. Or the other way around: if the index gets higher than the total amount of elements in a list, also wrap around to return the first element (better idea).
So in a fake session:
peter[~]$ newlisp
newLISP v.9.2.11 on Linux, execute 'newlisp -h' for more info.
> (set 'example '("a" "b" "c" "d" "e"))
("a" "b" "c" "d" "e")
>
> (length example)
5
> (nth 4 example)
"e"
> (nth 5 example)
"a"
> (nth 6 example)
"b"
> (nth 7 example)
"c"
> (nth 0 example)
"a"
> (nth -1 example)
"e"
> (nth -2 example)
"d"
> (nth -3 example)
"c"
What is the reason for the current logic of list indexing? To me it makes kind of an inconsistent impression :-) ...but probably I am missing a point...????
Cheers
Peter