summaryrefslogtreecommitdiffstats
path: root/alist-impl.scm
diff options
context:
space:
mode:
authorGravatar Arvydas Silanskas 2020-10-31 12:28:20 +0200
committerGravatar Arvydas Silanskas 2020-11-01 00:22:13 +0200
commit9734dea1013ba98d5bd09344e23f8d66065fdbc6 (patch)
treea51112a304875d8b49b378daa683a71179c777c0 /alist-impl.scm
parentMerge pull request #3 from arvyy/master (diff)
add depends; custom alist-delete; fix srfi-126 based impl
Diffstat (limited to 'alist-impl.scm')
-rw-r--r--alist-impl.scm27
1 files changed, 27 insertions, 0 deletions
diff --git a/alist-impl.scm b/alist-impl.scm
index fab350d..4946457 100644
--- a/alist-impl.scm
+++ b/alist-impl.scm
@@ -18,6 +18,33 @@
(lambda (e)
(pred (car e) (cdr e)))
alist))
+
+ (define (alist-delete key alist)
+ ;; find the tail of alist that will be kept
+ ;; ie rest entries after the last entry with matched key
+ (define kept-tail
+ (let loop ((tail alist)
+ (lst alist))
+ (cond
+ ((null? lst) tail)
+ (else
+ (if (equal? key (caar lst))
+ (loop (cdr lst) (cdr lst))
+ (loop tail (cdr lst)))))))
+ ;; if tail == alist; just return,
+ ;; else filter elements before the tail, and append the tail
+ (if (eq? alist kept-tail)
+ alist
+ (let loop ((lst alist)
+ (result/reversed '()))
+ (if (eq? lst kept-tail)
+ (append (reverse result/reversed) kept-tail)
+ (let* ((entry (car lst))
+ (keep? (not (equal? key (car entry))))
+ (result/reversed* (if keep?
+ (cons entry result/reversed)
+ result/reversed)))
+ (loop (cdr lst) result/reversed*))))))
(define (alist-search! alist key failure success)
(define (handle-success pair)