help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Wanted: Adding list-elements into an existing list


From: Pascal J. Bourguignon
Subject: Re: Wanted: Adding list-elements into an existing list
Date: Fri, 17 Oct 2008 00:32:02 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.2 (gnu/linux)

Nordlöw <per.nordlow@gmail.com> writes:

> Is there a variant of the function add-to-list(a b) where b can be a
> list aswell?

With add-to-list, the second argument can be anything you want.

(defvar my-list nil)
(add-to-list 'my-list '(a b c))
(add-to-list 'my-list '(1 2 3))
(add-to-list 'my-list '(a b c))
my-list --> ((1 2 3) (a b c))


> (add-list-elements-to-list '(a b c) '(c d e)) should evaluate '(a b c
> d e)

What about:

(defun add-to-list-several-elements (list-variable elements &optional append 
compare-fn)
  (dolist (element elements)
     (add-to-list list-variable element append compare-fn))
  (symbol-value list-variable))

(add-to-list-several-elements 'my-list '((1 2 3) (3 2 1)))
--> ((3 2 1) (1 2 3) (a b c))


> or do I have to implement it myself using an iteration-construct?

Yes.


However, add-to-list is not an example to follow.  It is not a
function, since it works by side effect.


It would be better to work with pure functions such as union.

(let ((my-list (list '(1 2 3) '(a b c))))
   (let ((augmented-list (union my-list (list '(1 2 3) '(3 2 1))
                                :test (function equal))))
      augmented-list))
--> ((3 2 1) (1 2 3) (a b c))






-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.


reply via email to

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