chickadee » alist-lib » alist-update!

alist-update! alist key functionprocedure
alist-update! alist key function thunkprocedure
alist-update! alist key function thunk =procedure

On analogy with hash-table-update!, descructively update an association.

alist
The alist to update
key
The key associated with the update
f
A monadic function taking the preƫxisting key
thunk
The thunk to apply if no association exists
=
The equality predicate for keys
(define alist-update!
  (case-lambda
    ((alist key function)
     (alist-update!
       alist
       key
       function
       (lambda () (error "Key not found -- ALIST-UPDATE!" key))))
    ((alist key function thunk) (alist-update! alist key function thunk eqv?))
    ((alist key function thunk =)
     (let ((pair (assoc key alist =)))
       (if pair
         (set-cdr! pair (function (cdr pair)))
         (alist-set! alist key (function (thunk))))))))