From d6760da67732419d8043d1fdc43e8081f96ef1f1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 22 Jul 2015 20:41:54 -0700 Subject: Adding nthcdr as accessor. * eval.c (eval_init): Register nthcdr function. * lib.c (nthcdr): New function. * lib.h (nthcdr): Declared. * share/txr/stdlib/place.tl (nthcdr): New defplace. * txr.1: Documented. --- lib.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 4d0c901b..6a662fbc 100644 --- a/lib.c +++ b/lib.c @@ -498,6 +498,19 @@ val last(val list) return nullocp(p) ? list : deref(p); } +val nthcdr(val pos, val list) +{ + cnum n = c_num(pos); + + if (n < 0) + uw_throwf(error_s, lit("nthcdr: negative index ~s given"), pos, nao); + + while (n-- > 0) + list = cdr(list); + + return list; +} + loc ltail(loc cons) { while (cdr(deref(cons))) -- cgit v1.2.3