University of Washington • CSE583 • D. Notkin © 2000
30
Another example
lFind a value associated with a given key in an association list (alist), a list of key-value pairs
l(define (assoc key alist)
l    (if (null? alist) #f
l      (let* ((pair (car alist))
l             (k (car pair))
l             (v (cdr pair)))
l         (if (equal? key k) v
l             (assoc key (cdr alist))))))
(define Zips (list ‘(98195 “Seattle”)
l                     ‘(15213 “Pittsburgh”)))
l  (assoc 98195 Zips) ® “Seattle”
l  (assoc 98103 Zips) ® #f