commit cabeb259346de43f4fa0cf69c1530cb253defce9
parent d2928e4eb046d1cb5515a4953759f3cb4e84c9d6
Author: Ed van Bruggen <ed@edryd.org>
Date: Sun, 12 Nov 2023 22:58:24 -0500
Negative nth number get element from end of list
Diffstat:
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/tib/core.tsp b/tib/core.tsp
@@ -252,11 +252,13 @@
; TODO make nth generic for list str vec, made up of list-ref vec-ref str-ref
(def (nth lst n)
- "Element number n of list"
+ "Element number n of list, starting from 0
+ If negative get number from end of list"
(cond
((atom? lst)
(error 'nth "index of list out of bounds"))
- ((<= n 0) (car lst))
+ ((< n 0) (nth lst (+ (length lst) n)))
+ ((= n 0) (car lst))
(else (nth (cdr lst) (- n 1)))))
; TODO diff name head/tail since conflicts w/ unix