[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: alist and multiple values for one key
From: |
Friedrich Dominicus |
Subject: |
Re: alist and multiple values for one key |
Date: |
20 Jan 2003 17:03:09 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Native Windows TTY Support) |
ncauderan@hotmail.com (Norbert C.) writes:
> Hi,
>
> I would lile to find the "good way" to retrieve multiple values
> for a given key in an alist.
>
> But I didn't find a natural way to do it. For example :
> (setq trees '((pine . cones) (pine . acorns) (oak . acorns) (maple .
> seeds)))
> ==> ((pine . cones) (pine . acorns) (oak . acorns) (maple . seeds))
> (assoc 'pine trees)
That does not work this way. assoc can only be used with a symbol but
if you step into the list you just have other lists.
This code will work for the given data:
(defun collect-if (pred list)
(mapcar #'(lambda (el) (when (funcall pred el) el)) list))
you can call it with
(collect-if #'(lambda (item) (eq (car item) 'pine)) trees)
((pine . cones) (pine . acorns))
Regards
Friedrich