chickadee » alist-lib » alist-ref

alist-ref alist keyprocedure
alist-ref alist key thunkprocedure
alist-ref alist key thunk =procedure

Return a value associated with its key or apply thunk.

alist
The alist to search in
key
The key whose value to return
thunk
The thunk to apply when association doesn't exist (default is to err)
=
The equality predicate to apply to keys
(define alist-ref
  (case-lambda
    ((alist key)
     (alist-ref
       alist
       key
       (lambda () (error "Key not found -- ALIST-REF" key))))
    ((alist key thunk) (alist-ref alist key thunk eqv?))
    ((alist key thunk =)
     (let ((value (assoc key alist =))) (if value (cdr value) (thunk))))))