chickadee » srfi-116 » iassoc

iassoc key ialist #!optional =procedure
iassq key ialistprocedure
iassv key ialistprocedure

ialist must be an immutable association list — an ilist of ipairs. These procedures find the first ipair in ialist whose icar field is key, and returns that ipair. If no ipair in ialist has key as its icar, then #f is returned. iassq uses eq? to compare key with the icar fields of the ipairs in ialist, while iassv uses eqv? and iassoc uses equal?.

(define e (iq (a 1) (b 2) (c 3)))
(iassq 'a e)                               ;=>  (a 1)
(iassq 'b e)                               ;=>  (b 2)
(iassq 'd e)                               ;=>  #f
(iassq (ilist 'a) (iq ((a)) ((b)) ((c))))  ;=>  #f
(iassoc '(a) (ilist '((a)) '((b)) '((c)))) ;=>  ((a))
(iassq 5 (iq (2 3) (5 7) (11 13)))         ;=>  *unspecified*
(iassv 5 (iq (2 3) (5 7) (11 13)))         ;=>  (5 7)

The comparison procedure is used to compare the elements ei of ilist to the key parameter in this way:

(= key (icar ei)) ; ilist is (E1 ... En)

That is, the first argument is always key, and the second argument is one of the ilist elements. Thus one can reliably find the first entry of ialist whose key is greater than five with (iassoc 5 ialist <).

Note that fully general ialist searching may be performed with the ifind-tail and ifind procedures, e.g.

;; Look up the first association in ialist with an even key:
(ifind (lambda (a) (even? (icar a))) ialist)