Hi all!
it's probably been discussed before but i haven't searched enough (i got the result on negative offset for slice)
is there any easy way to get negative counter act as position?
inclusive start and end
like:
(set 'a '(1 2 3 4 5 6 7))
(2 -2 a) ;want (3 4 5 6), newlisp gives (3 4 5 6 7)
in code:
(define (my-slice l s c)
(if (>= c 0)
(slice l s c)
(slice l s (+ (length l) c -1))))
a; (1 2 3 4 5 6 7)
(my-slice a 2 -2) ;gives (3 4 5 6)
(my-slice a 2 0); gives ()
(my-slice a 2 2); gives (3 4)
it would be nice to have it in implicit slicing too like (2 -2 a)
thx!
slice negative counter
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
it's inclusive in left endpoint and non-inclusive at the right endpoint, similar to pythoncormullion wrote:looks like you can now do this!
9.1.11 release notes
negative second length in slicing now interpreted as offset
from the right: (2 3 "abcdefg") => "cde", (2 -3 "abcdefg") => "cd"
before negative length would be interpreted as going to the end
>>> a="abcdefg"
>>> a[2:-3]
'cd'
>>> a[2:]
'cdefg'
>>> a[2:0]
''
thanks Lutz!
-
- Posts: 72
- Joined: Sun Jun 11, 2006 8:02 pm
- Location: berkeley, california
- Contact:
v9.1.11 slice
> (slice '(a b c d e f) 2 -1)
(c d e)
> (slice '(a b c d e f) 2 )
(c d e f)
Second one seems to be correct.
Both should give same results, no?
(c d e)
> (slice '(a b c d e f) 2 )
(c d e f)
Second one seems to be correct.
Both should give same results, no?
-
- Posts: 72
- Joined: Sun Jun 11, 2006 8:02 pm
- Location: berkeley, california
- Contact: