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'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 2 - Page 22 : 46
Functional Programming in Scheme
Expressions, Types, and Functions
Association lists

An association list is a list of cons pairs

Association lists are used in the same way as associative arrays



(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) 
((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 list and property lists