From bc85ec12271ecc273cb36e7ef35d071c364e46ab Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 16 Nov 2015 06:45:13 -0800 Subject: Provide butlast function. * eval.c (eval_init): Registered butlast intrinsic. * lib.c (butlast): New function. * lib.h (butlast): Declared. * txr.1: Documented butlast. --- eval.c | 1 + lib.c | 5 +++++ lib.h | 1 + txr.1 | 24 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/eval.c b/eval.c index fd5a720f..b1bb0d17 100644 --- a/eval.c +++ b/eval.c @@ -4471,6 +4471,7 @@ void eval_init(void) reg_fun(intern(lit("reverse"), user_package), func_n1(reverse)); reg_fun(intern(lit("ldiff"), user_package), func_n2(ldiff)); reg_fun(intern(lit("last"), user_package), func_n1(last)); + reg_fun(intern(lit("butlast"), user_package), func_n1(butlast)); reg_fun(intern(lit("nthcdr"), user_package), func_n2(nthcdr)); reg_fun(intern(lit("flatten"), user_package), func_n1(flatten)); reg_fun(intern(lit("flatten*"), user_package), func_n1(lazy_flatten)); diff --git a/lib.c b/lib.c index 09e85693..580b2c2c 100644 --- a/lib.c +++ b/lib.c @@ -7396,6 +7396,11 @@ val dwim_del(val seq, val ind_range) } } +val butlast(val seq) +{ + return sub(seq, zero, negone); +} + val update(val seq, val fun) { switch (type(seq)) { diff --git a/lib.h b/lib.h index 153492d9..3b10ce6d 100644 --- a/lib.h +++ b/lib.h @@ -905,6 +905,7 @@ val ref(val seq, val ind); val refset(val seq, val ind, val newval); val dwim_set(val seq, val ind_range, val newval); val dwim_del(val seq, val ind_range); +val butlast(val seq); val replace(val seq, val items, val from, val to); val update(val seq, val fun); val search(val seq, val key, val from, val to); diff --git a/txr.1 b/txr.1 index 281bc167..dbb819d9 100644 --- a/txr.1 +++ b/txr.1 @@ -20103,6 +20103,30 @@ is returned, which may be .meta sequence itself or a copy. +.coNP Function @ butlast +.synb +.mets (butlast << sequence ) +.syne +.desc +The +.code butlast +function returns the prefix of +.meta sequence +consisting of a copy of it, with the last item omitted. +If +.meta sequence +is empty, an empty sequence is returned. + +Dialect note: the Common Lisp function +.code nbutlast +is not provided. The \*(TL +.code take +function provides the same functionality for lists (only with the +arguments reversed relative to +.codn nbutlast ), +and additionally provides lazy semantics, and works with vectors +and strings. + .coNP Function @ search .synb .mets (search < haystack < needle >> [ testfun <> [ keyfun ]) -- cgit v1.2.3