Function: assoc
assoc is a function defined in fns.c.
Signature
(assoc KEY ALIST &optional TESTFN)
Documentation
Return non-nil if KEY is equal to the car of an element of ALIST.
The value is actually the first element of ALIST whose car equals KEY.
Equality is defined by the function TESTFN, defaulting to equal.
TESTFN is called with 2 arguments: a car of an alist element and KEY.
Other relevant functions are documented in the list and alist groups.
Probably introduced at or before Emacs version 19.29.
Shortdoc
;; alist
(assoc 'foo '((foo . bar) (zot . baz)))
=> (foo . bar)
;; list
(assoc "b" '(("a" . 1) ("b" . 2)))
=> ("b" . 2)
Source Code
// Defined in /usr/src/emacs/src/fns.c
{
if (eq_comparable_value (key) && NILP (testfn))
return Fassq (key, alist);
Lisp_Object tail = alist;
FOR_EACH_TAIL (tail)
{
Lisp_Object car = XCAR (tail);
if (!CONSP (car))
continue;
if ((NILP (testfn)
? (EQ (XCAR (car), key) || !NILP (Fequal
(XCAR (car), key)))
: !NILP (calln (testfn, XCAR (car), key))))
return car;
}
CHECK_LIST_END (tail, alist);
return Qnil;
}