1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-16 16:50:21 +02:00

srfi-1 remove!: move from C to Scheme

* libguile/srfi-1.c (scm_srfi1_remove_x): delete.
* libguile/srfi-1.h (scm_srfi1_remove_x): delete.
* module/srfi/srfi-1.scm: add remove!.
This commit is contained in:
Rob Browning 2024-07-16 22:57:07 -05:00
parent 03d4a3b5df
commit c5f26d4c27
3 changed files with 21 additions and 30 deletions

View file

@ -782,6 +782,27 @@ a common tail with @{list}."
(lp (cdr lst) (cdr lst) new-tail))))
(lp (cdr lst) last-kept tail))))))))
(define (remove! pred lst)
"Return a list containing all elements from @var{list} which do not
satisfy the predicate @var{pred}. The elements in the result list have
the same order as in @var{list}. The order in which @var{pred} is
applied to the list elements is not specified. @var{list} may be
modified to build the return list."
(cond
((null? lst) lst)
((pred (car lst)) (remove! pred (cdr lst)))
(else
(let lp ((prev lst))
(let ((next (cdr prev)))
(if (null? next)
lst
(let ((x (car next)))
(if (pred x)
(begin
(set-cdr! prev (cdr next))
(lp prev))
(lp next)))))))))
;;; Searching