summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-26 06:41:57 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-26 06:41:57 -0700
commitc2d87ddcd9e18d7088448533ba8203cd9a8a042e (patch)
treeec4a0ec4f732fef9c9cd87ab3276ece7a407673b /lib.c
parentbc0d27c80e2f7b534ba1efd3f60210b5855f65c1 (diff)
downloadtxr-c2d87ddcd9e18d7088448533ba8203cd9a8a042e.tar.gz
txr-c2d87ddcd9e18d7088448533ba8203cd9a8a042e.tar.bz2
txr-c2d87ddcd9e18d7088448533ba8203cd9a8a042e.zip
last, butlast: become accessors, get optional arg.
* eval.c (optimize_qquote_form): Pass nil to default new argument of butlast. (me_whilet, me_iflet_whenlet): Likewise for last. (eval_init): Add optional argument to registration of last and butlast intrinsics. * lib.c (last, butlast): Support optional numeric argument, like in Common Lisp. * lib.h (last, butlast): Declarations updated. * share/txr/stdlib/place.tl (last, butlast): New place macros. * txr.1: Updated documentation. The description of last is now moved into the sequence functions section.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/lib.c b/lib.c
index 0beb76f4..fffe2970 100644
--- a/lib.c
+++ b/lib.c
@@ -575,13 +575,21 @@ loc lastcons(val list)
return ret;
}
-val last(val seq)
+val last(val seq, val n)
{
- if (listp(seq)) {
- loc p = lastcons(seq);
- return nullocp(p) ? seq : deref(p);
+ if (null_or_missing_p(n)) {
+ if (listp(seq)) {
+ loc p = lastcons(seq);
+ return nullocp(p) ? seq : deref(p);
+ }
+ return sub(seq, negone, t);
+ } else {
+ if (listp(seq))
+ return nthlast(n, seq);
+ return if3(plusp(n),
+ sub(seq, neg(n), t),
+ sub(seq, t, t));
}
- return sub(seq, negone, t);
}
val nthcdr(val pos, val list)
@@ -8414,9 +8422,14 @@ val dwim_del(val seq, val ind_range)
}
}
-val butlast(val seq)
+val butlast(val seq, val idx)
{
- return sub(seq, zero, negone);
+ if (listp(seq)) {
+ return butlastn(default_arg(idx, one), seq);
+ } else {
+ val nidx = if3(null_or_missing_p(idx), negone, neg(idx));
+ return sub(seq, zero, if3(plusp(nidx), zero, nidx));
+ }
}
val update(val seq, val fun)