Lecture overview -- Keyboard shortcut: 'u'  Previous page: List functions -- Keyboard shortcut: 'p'  Next page: Property lists -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 1 - Page 16 : 49
Programming Paradigms
Introduction to Functional Programming in Scheme
Association lists

An association list is a list of cons pairs

Association lists are used in the same way as associative arrays

Expression

Value

(define computer-prefs 
 '((peter . windows) (lars . mac)
   (paw . linux) (kurt . unix)))
(assq 'lars computer-prefs)
(lars . mac)
(assq 'kurt computer-prefs)
(kurt . unix)
(define computer-prefs-1
 (cons (cons 'lene 'windows) 
       computer-prefs))
computer-prefs-1
((lene . windows)
 (peter . windows) 
 (lars . mac)
 (paw . linux)
 (kurt . unix))

Examples of association lists. The function assq uses eq? to compare the first parameter with the first element - the key element - in the pairs. As an alternative, we could use the function assoc, which uses equal? for comparison. A better and more general solution would be to pass the comparison function as parameter. Notice in this context, that both assq and assoc are 'traditional Lisp functions' and part of Scheme, as defined in the language report.

Go to exerciseCreation of association lists
Go to exerciseAssociation lists and property lists