pairlis
pairlis Function
Syntax:
pairlis keys data &optional alist → new-alist
Arguments and Values:
keys—a proper list.
data—a proper list.
alist—an association list. The default is the empty list.
new-alist—an association list.
Description:
Returns an association list that associates elements of keys to corresponding elements of data. The consequences are undefined if keys and data are not of the same length.
If alist is supplied, pairlis returns a modified alist with the new pairs prepended to it. The new pairs may appear in the resulting association list in either forward or backward order. The result of
(pairlis ’(one two) ’(1 2) ’((three . 3) (four . 19)))
might be
((one . 1) (two . 2) (three . 3) (four . 19))
or
((two . 2) (one . 1) (three . 3) (four . 19))
Examples:
(setq keys ’(1 2 3)
data ’("one" "two" "three")
alist ’((4 . "four"))) → ((4 . "four"))
(pairlis keys data) → ((3 . "three") (2 . "two") (1 . "one"))
(pairlis keys data alist)
→ ((3 . "three") (2 . "two") (1 . "one") (4 . "four"))
alist → ((4 . "four"))
Exceptional Situations:
Should be prepared to signal an error of type type-error if keys and data are not proper lists.
See Also:
aconsExpanded Reference: pairlis
Basic usage
pairlis constructs an association list by pairing up corresponding elements from a keys list and a data list.
(pairlis '(a b c) '(1 2 3))
=> ((C . 3) (B . 2) (A . 1))
Prepending to an existing alist
An optional third argument provides an existing alist to append the new pairs onto.
(pairlis '(x y) '(10 20) '((z . 30)))
=> ((Y . 20) (X . 10) (Z . 30))
Building a simple symbol table
(let ((vars '(width height depth))
(vals '(100 200 50)))
(pairlis vars vals))
=> ((DEPTH . 50) (HEIGHT . 200) (WIDTH . 100))
The original alist is not modified
(let ((existing '((old . value))))
(pairlis '(new) '(data) existing)
existing)
=> ((OLD . VALUE))
Looking up values in the result
The resulting alist works with assoc as expected.
(let ((env (pairlis '(name age) '("Alice" 30))))
(values (cdr (assoc 'name env))
(cdr (assoc 'age env))))
=> "Alice"
=> 30