[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: add-to-plist, add-plist-to-plist functions

From: Karl Chen
Subject: Re: add-to-plist, add-plist-to-plist functions
Date: Sun, 28 Apr 2002 16:45:56 -0700

Oops! You're right, thanks. Here's the same code with names changed.

(defun add-to-alist (alist-var p)
  "Add an element to an alist symbol.

If the element is a cons:
  If there exists an element in the value of ALIST-VAR whose car matches the
new element's car, replace its cdr. Else prepend the whole element to the

If the element is not a cons:
  If the element does not already exist, prepend it. (Equivalent to
`add-to-list' in this case.)"
  (if (consp p)
      (let ((alist (symbol-value alist-var))
            (pname (car p)))
        (when pname
          (while alist
            (let ((p0 (car alist)))
              (setq alist (cdr alist))
              (when (and (consp p0) (eq (car p0) pname))
                (setcdr p0 (cdr p))
            (setq alist nil)
            (setq pname nil))))
          (when pname
            (set alist-var (cons p (symbol-value alist-var)))
    (add-to-list alist-var p)))

(defun add-alist-to-alist (alist new-alist)
  "Apply NEW-ALIST on ALIST by calling `add-to-alist' on each entry."
  (while new-alist
    (add-to-alist 'alist (car new-alist))
    (setq new-alist (cdr new-alist))

"Miles Bader" <address@hidden> wrote in message
> "Karl Chen" <address@hidden> writes:
> > Hi, I wrote these two functions which may be useful to others:
> Your terminology is incorrect -- looking at your code, it seems to be
> manipulating `alists', not `plists'.
> [An alist is a list of cons cells, with the car of each being the key,
> and the cdr the value; a plist is a list of alternating keys and values.]
> -Miles
> --
> Run away!  Run away!

reply via email to

[Prev in Thread] Current Thread [Next in Thread]