diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-10-25 06:39:58 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-10-25 06:39:58 -0700 |
commit | dcd3ef3f78ee3f17d7c706ccaa1ff74c5dc7f104 (patch) | |
tree | 21ca552d490ee18594391b5d3435abb0c2feedb7 /lib.c | |
parent | 95abf5aa1cb792bdcb472399d1ef9dfc9b9088e0 (diff) | |
download | txr-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.c | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -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))) |