Re: Making alistp matching plistp requirements

tpeplt <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

> plistp is defined as
>
> (defun plistp (object)
>   "Non-nil if and only if OBJECT is a valid plist."
>   (declare (pure t) (side-effect-free error-free))
>   (let ((len (proper-list-p object)))
>     (and len (zerop (% len 2)))))
>
> For an equivalent alistp, checking cons cells with key and value,
> how would the function be?
>

The answer is going to depend on how strict you want your
definition of an association list to be.

Here is a simple definition that does not check the contents
of the items in the alist:

  (defun alistp (obj-list)
    "Determine whether OBJ-LIST is an association list.
     This function definition says that an empty list is not
     an association list."
    (when (and obj-list (proper-list-p obj-list))
      (cl-every #'consp obj-list)))

You might decide to have additional, more strict conditions,
in which case you could replace ‘#'consp’ with
‘#'your-alist-item-p’.

   (defun your-alist-item-p (pair)
      (and (consp pair)
           ... ;; your additional conditions))

For example, (a . nil) and (a) are the same, so they are a
valid key/value pair, but you might want to restrict your
alist pairs not to include nil as a possible value; likewise
with not allowing nil as a key: (nil . some-value)

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.