Scheme Programmer's Manual
assq, assv, assoc, assp - association list lookup
(import (rnrs)) ;R6RS
(import (rnrs lists)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
(assq obj alist)
(assv obj alist)
(assoc obj alist)
(assp proc alist) ;R6RS
(assoc obj alist compare) ;R7RS
These procedures find the first pair in
whose car field satisfies a given condition, and returns that pair
If no pair in
satisfies the condition, then #f is returned.
- assoc, assv, and assq
These procedures look for a pair that has
as its car. The
with the car fields of the pairs in
- assp (R6RS)
procedure successively applies
to the car fields of alist and looks for a pair for which it returns a
called in the same dynamic environment as
should accept one argument and return a single value.
should not mutate
- assoc (R7RS)
if given and
otherwise. There is nothing said about the
procedure in R7RS. Note in particular that its argument order is not
specified; it might be called with the object as the first or second
Returns a pair from
(define img '((src "lambda.png") (width "100") (height "120")))
(assq 'src img) => (src "lambda.png")
(assq 'width img) => (width "100")
(assq 'alt img) => #f
; Common pattern to look up a value using a default
(cond ((assq 'style img) => cadr)
(else "")) => ""
; Newly allocated pairs are not eq? to other pairs
(assq (list 'a) '(((a)) ((b)) ((c))))
; Instead use equal? to compare based on the structure
(assoc (list 'a) '(((a)) ((b)) ((c))))
; Behavior of eq? and assq on numbers is not specified
(assq 5 '((2 3) (5 7) (11 13)))
; So use eqv? and assv instead
(assv 5 '((2 3) (5 7) (11 13)))
=> (5 7)
; Custom tests can be done with assp in R6RS.
(define d '((3 a) (1 b) (4 c)))
(assp even? d) => (4 c)
(assp odd? d) => (3 a)
; Assp in R6RS provides a test and the optional compare
; procedure in R7RS provides a comparison. Using it as
; test is possible, but awkward.
(define d '((3 a) (1 b) (4 c)))
(assoc #f d (lambda (x y) (even? (or x y))))
=> (4 c)
(assoc #f d (lambda (x y) (odd? (or x y))))
=> (3 a)
; When a custom comparison is needed instead of a test,
; R7RS/SRFI-1 is more elegant than in the above example:
(define d '((1 1) (2 4) (3 9)))
(assoc 2.0 d =) => (2 4)
(assoc 2.0 d <) => unspecified in R7RS
(assoc 2.0 d <) => (3 9) ; in SRFI-1
Association lists are often used as a lightweight alternative to hash
tables. They have the benefit of being printable as S-expressions. The
linear cost of lookup is balanced by the lower constant cost when the
number of keys is small.
procedure is appropriate when keys are symbols (or when you would
otherwise compare the keys with
When the keys are characters or numbers, use
When the keys are strings, pairs, vectors, bytevectors or a variation
of types so far mentioned, use
When the keys are other types, use
with a compare procedure.
Although they are ordinarily used as predicates, these procedures do
not have question marks in their names because they return useful
values rather than just #t or #f.
R4RS and R5RS have
and the same
as R6RS. R7RS modified
to give it an optional comparison procedure in the SRFI-1 tradition.
R6RS added the
R6RS specifies that these procedures do not traverse
further once a matching pair has been found. In practice this is how it will
be implemented in non-R6RS implementations as well.
Association lists are common in Lisp dialects.
in Common Lisp returns NIL instead of #f when there is no matching
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain,
was not a list of pairs, or
was not a procedure that accepts one argument and returns one value.
The assertions described above are errors.
Implementations may signal an error, extend the procedure's
domain of definition to include such arguments,
or fail catastrophically.
A variant of
that returns the cadr of the matching pair appeared in
Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I
It was used to represent environments in
This page is part of the
It includes materials from the RnRS documents.
More information can be found at
Return to Main Contents
- RETURN VALUES
- APPLICATION USAGE
- SEE ALSO