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

assq

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))
 
Unlike assoc, assq uses eq? to identify keys. Therefore, keys are limited to symbols when using assq.

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

Model:

(define (assq x a)
  (cond ((null? a) #f)
    ((eq? (caar a) x) (car a))
    (#t (assq x (cdr a)))))

Implementation:

(define (assq x a)
  (letrec
    ((_assq (lambda (a)
      (cond ((null? a) #f)
        ((eq? (caar a) x) (car a))
        (#t (_assq (cdr a)))))))
    (_assq a)))

Example:

(assq 'c '((a . i) (b . ii) (c . iii) (d . iv))) 
=> (c . iii)

See also:
assoc, memq.