summaryrefslogtreecommitdiffstats
path: root/share
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 /share
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 'share')
-rw-r--r--share/txr/stdlib/place.tl20
1 files changed, 20 insertions, 0 deletions
diff --git a/share/txr/stdlib/place.tl b/share/txr/stdlib/place.tl
index 9bad42fa..aa534e4a 100644
--- a/share/txr/stdlib/place.tl
+++ b/share/txr/stdlib/place.tl
@@ -887,3 +887,23 @@
(define-place-macro eighth (obj) ^(ref ,obj 7))
(define-place-macro ninth (obj) ^(ref ,obj 8))
(define-place-macro tenth (obj) ^(ref ,obj 9))
+
+(define-place-macro last (obj : (n nil have-n))
+ (cond
+ ((and have-n (constantp n) (not (plusp n)))
+ ^(sub ,obj t t))
+ ((and have-n (constantp n))
+ ^(sub ,obj ,(- n) t))
+ (have-n
+ ^(sub ,obj (- (max ,n 0)) t))
+ (t ^(sub ,obj -1 t))))
+
+(define-place-macro butlast (obj : (n nil have-n))
+ (cond
+ ((and have-n (constantp n) (not (plusp n)))
+ obj)
+ ((and have-n (constantp n))
+ ^(sub ,obj 0 ,(- n)))
+ (have-n
+ ^(sub ,obj 0 (- (max ,n 0))))
+ (t ^(sub ,obj 0 -1))))