unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Wanted: Adding list-elements into an existing list
@ 2008-10-16 13:13 Nordlöw
  2008-10-16 13:16 ` Joost Diepenmaat
  2008-10-16 22:32 ` Pascal J. Bourguignon
  0 siblings, 2 replies; 8+ messages in thread
From: Nordlöw @ 2008-10-16 13:13 UTC (permalink / raw
  To: help-gnu-emacs

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

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

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

/Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: Adding list-elements into an existing list
  2008-10-16 13:13 Wanted: Adding list-elements into an existing list Nordlöw
@ 2008-10-16 13:16 ` Joost Diepenmaat
  2008-10-16 13:36   ` Nordlöw
  2008-10-16 22:32 ` Pascal J. Bourguignon
  1 sibling, 1 reply; 8+ messages in thread
From: Joost Diepenmaat @ 2008-10-16 13:16 UTC (permalink / raw
  To: help-gnu-emacs

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?
>
> (add-list-elements-to-list '(a b c) '(c d e)) should evaluate '(a b c
> d e)
>
> or do I have to implement it myself using an iteration-construct?

That's what (append) does:

  append is a built-in function in `C source code'.

  (append &rest SEQUENCES)

  Concatenate all the arguments and make the result a list.  The result
  is a list whose elements are the elements of all the arguments.  Each
  argument may be a list, vector or string.  The last argument is not
  copied, just used as the tail of the new list.


-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: Adding list-elements into an existing list
  2008-10-16 13:16 ` Joost Diepenmaat
@ 2008-10-16 13:36   ` Nordlöw
  2008-10-16 15:43     ` Drew Adams
  2008-10-17  0:28     ` Barry Margolin
  0 siblings, 2 replies; 8+ messages in thread
From: Nordlöw @ 2008-10-16 13:36 UTC (permalink / raw
  To: help-gnu-emacs

On 16 Okt, 15:16, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> Nordlöw <per.nord...@gmail.com> writes:
> > Is there a variant of the function add-to-list(a b) where b can be a
> > list aswell?
>
> > (add-list-elements-to-list '(a b c) '(c d e)) should evaluate '(a b c
> > d e)
>
> > or do I have to implement it myself using an iteration-construct?
>
> That's what (append) does:
>
>   append is a built-in function in `C source code'.
>
>   (append &rest SEQUENCES)
>
>   Concatenate all the arguments and make the result a list.  The result
>   is a list whose elements are the elements of all the arguments.  Each
>   argument may be a list, vector or string.  The last argument is not
>   copied, just used as the tail of the new list.
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/

(append) does not modify its first argument which is what I want.

I can solve it through
  (setq x (append x y))

but before I create this new function for this I just wanted to make
sure that no such function already exists....

/Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Wanted: Adding list-elements into an existing list
  2008-10-16 13:36   ` Nordlöw
@ 2008-10-16 15:43     ` Drew Adams
  2008-10-16 16:02       ` Allan Gottlieb
  2008-10-17  0:28     ` Barry Margolin
  1 sibling, 1 reply; 8+ messages in thread
From: Drew Adams @ 2008-10-16 15:43 UTC (permalink / raw
  To: 'Nordlöw', help-gnu-emacs

> (append) does not modify its first argument which is what I want.
> I can solve it through (setq x (append x y))

Depends what you want. You mentioned `add-to-list', which adds an element only
if it is not already there. Neither `append' nor `nconc' does that for combining
lists.

Sounds more like you want something like set intersection: see functions
`intersection' or `nintersection' in library `cl-seq.el'.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: Adding list-elements into an existing list
  2008-10-16 15:43     ` Drew Adams
@ 2008-10-16 16:02       ` Allan Gottlieb
  2008-10-16 16:12         ` Drew Adams
  0 siblings, 1 reply; 8+ messages in thread
From: Allan Gottlieb @ 2008-10-16 16:02 UTC (permalink / raw
  To: help-gnu-emacs

At Thu, 16 Oct 2008 08:43:48 -0700 Drew Adams <drew.adams@oracle.com> wrote:

>> (append) does not modify its first argument which is what I want.
>> I can solve it through (setq x (append x y))
>
> Depends what you want. You mentioned `add-to-list', which adds an element only
> if it is not already there. Neither `append' nor `nconc' does that for combining
> lists.
>
> Sounds more like you want something like set intersection: see functions
> `intersection' or `nintersection' in library `cl-seq.el'.

Sounds more like set union to me.
allan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Wanted: Adding list-elements into an existing list
  2008-10-16 16:02       ` Allan Gottlieb
@ 2008-10-16 16:12         ` Drew Adams
  0 siblings, 0 replies; 8+ messages in thread
From: Drew Adams @ 2008-10-16 16:12 UTC (permalink / raw
  To: 'Allan Gottlieb', help-gnu-emacs

> >> (append) does not modify its first argument which is what I want.
> >> I can solve it through (setq x (append x y))
> >
> > Depends what you want. You mentioned `add-to-list', which 
> adds an element only
> > if it is not already there. Neither `append' nor `nconc' 
> does that for combining
> > lists.
> >
> > Sounds more like you want something like set intersection: 
> > see functions `intersection' or `nintersection' in library `cl-seq.el'.
> 
> Sounds more like set union to me.

Oooooops! (another espresso, please)

Yes - `union' or `nunion'.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: Adding list-elements into an existing list
  2008-10-16 13:13 Wanted: Adding list-elements into an existing list Nordlöw
  2008-10-16 13:16 ` Joost Diepenmaat
@ 2008-10-16 22:32 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2008-10-16 22:32 UTC (permalink / raw
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: Adding list-elements into an existing list
  2008-10-16 13:36   ` Nordlöw
  2008-10-16 15:43     ` Drew Adams
@ 2008-10-17  0:28     ` Barry Margolin
  1 sibling, 0 replies; 8+ messages in thread
From: Barry Margolin @ 2008-10-17  0:28 UTC (permalink / raw
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 750 bytes --]

In article 
<f3c2bc36-6120-4567-9297-3e91bf32e350@u46g2000hsc.googlegroups.com>,
 Nordlöw <per.nordlow@gmail.com> wrote:

> (append) does not modify its first argument which is what I want.
> 
> I can solve it through
>   (setq x (append x y))
> 
> but before I create this new function for this I just wanted to make
> sure that no such function already exists....

nconc is like append, but modifies the first argument.

But you still have to use (setq x (nconc x y)), in case x is initially 
the empty list, because you can't modify the empty list in place.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-10-17  0:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-16 13:13 Wanted: Adding list-elements into an existing list Nordlöw
2008-10-16 13:16 ` Joost Diepenmaat
2008-10-16 13:36   ` Nordlöw
2008-10-16 15:43     ` Drew Adams
2008-10-16 16:02       ` Allan Gottlieb
2008-10-16 16:12         ` Drew Adams
2008-10-17  0:28     ` Barry Margolin
2008-10-16 22:32 ` Pascal J. Bourguignon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).