summaryrefslogtreecommitdiffstats
path: root/alist-impl.scm
diff options
context:
space:
mode:
authorGravatar John Cowan 2021-06-24 19:24:32 -0400
committerGravatar John Cowan 2021-06-24 19:24:32 -0400
commit570845a2289b1feffd89928e8472000cd79b723a (patch)
tree213302b088d6686adedf17d3d2d4e80280a89bc0 /alist-impl.scm
parentadded spec (diff)
parentUpdate spec.md (diff)
Merge remote-tracking branch 'origin/master'
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)