summaryrefslogtreecommitdiffstats
path: root/alist-impl.scm
diff options
context:
space:
mode:
authorGravatar John Cowan 2020-10-31 19:10:31 -0400
committerGravatar GitHub 2020-10-31 19:10:31 -0400
commit814ff1945faffadd8a636e00bffb7cb18056adab (patch)
treea51112a304875d8b49b378daa683a71179c777c0 /alist-impl.scm
parentMerge pull request #3 from arvyy/master (diff)
parentadd depends; custom alist-delete; fix srfi-126 based impl (diff)
Merge pull request #4 from arvyy/master
Fix srfi-126 base dictionary implementation, add tail-sharing alist-delete proc, add "depends on" clauses
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)