diff options
| author | 2020-10-31 12:28:20 +0200 | |
|---|---|---|
| committer | 2020-11-01 00:22:13 +0200 | |
| commit | 9734dea1013ba98d5bd09344e23f8d66065fdbc6 (patch) | |
| tree | a51112a304875d8b49b378daa683a71179c777c0 /alist-impl.scm | |
| parent | Merge 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.scm | 27 |
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) |
