t3x.org / sketchy / library / assoc.html
SketchyLISP
Reference
  Copyright (C) 2006
Nils M Holm

assoc

Conformance: R5RS

Purpose: Retrieve a binding from an association list. An association list is a list of pairs where the car part of each pair holds a key and the cdr part of the pair holds the value associated with that key:
((key1 . value1) ... (keyN . valueN))

Arguments:
X - key of value to be found
A - association list

Model:

(define (assoc x a)
  (cond ((null? a) #f)
    ((equal? (caar a) x) (car a))
    (#t (assoc x (cdr a)))))

Implementation:

(define (assoc x a)
  (letrec
    ((_assoc (lambda (a)
      (cond ((null? a) #f)
        ((equal? (caar a) x) (car a))
        (#t (_assoc (cdr a)))))))
    (_assoc a)))

Example:

(assoc '3 '((1 . i) (2 . ii) (3 . iii) (4 . iv))) 
=> (3 . iii)

See also:
member, assq.