summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
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)))