summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-25 06:39:58 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-25 06:39:58 -0700
commitdcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104 (patch)
tree21ca552d490ee18594391b5d3435abb0c2feedb7 /lib.c
parent95abf5aa1cb792bdcb472399d1ef9dfc9b9088e0 (diff)
downloadtxr-dcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104.tar.gz
txr-dcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104.tar.bz2
txr-dcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104.zip
New accessors nthlast and butlastn.
* eval.c (eval_init): register nthlast and butlastn intrinsicis. * lib.c (nthlast, butlastn): New function. * lib.h (nthlast, butlastn): Declared. * share/txr/stdlib/place.tl (defplace nthlast, defplace butlastn): New places. * txr.1: Documented nthlast and butlastn.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 5a65e6c7..0beb76f4 100644
--- a/lib.c
+++ b/lib.c
@@ -599,6 +599,37 @@ val nthcdr(val pos, val list)
return list;
}
+val nthlast(val pos, val list)
+{
+ val iter = list;
+
+ while (plusp(pos) && consp(list)) {
+ list = cdr(list);
+ pos = pred(pos);
+ }
+
+ if (plusp(pos))
+ return iter;
+
+ if (list == iter) {
+ while (consp(iter))
+ iter = cdr(iter);
+ } else {
+ while (consp(list)) {
+ iter = cdr(iter);
+ list = cdr(list);
+ }
+ }
+
+ return iter;
+}
+
+val butlastn(val n, val list)
+{
+ val tail = nthlast(n, list);
+ return ldiff(list, tail);
+}
+
loc ltail(loc cons)
{
while (cdr(deref(cons)))