all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Adding Lists/Sequences
@ 2008-09-23 11:46 Nordlöw
  2008-09-23 11:52 ` Joost Kremers
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Nordlöw @ 2008-09-23 11:46 UTC (permalink / raw)
  To: help-gnu-emacs

Is there a general function, say foo, that adds lists or, even better,
sequences together?

I want this
  (foo '("a" "b") '("c" "d"))
to evaluate to
  '("a" "b" "c" "d")

Thanks in advance,
Nordlöw


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

* Re: Adding Lists/Sequences
  2008-09-23 11:46 Adding Lists/Sequences Nordlöw
@ 2008-09-23 11:52 ` Joost Kremers
  2008-09-23 13:22   ` Nordlöw
  2008-09-23 15:06 ` Tassilo Horn
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 20+ messages in thread
From: Joost Kremers @ 2008-09-23 11:52 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw wrote:
> Is there a general function, say foo, that adds lists or, even better,
> sequences together?
>
> I want this
>   (foo '("a" "b") '("c" "d"))
> to evaluate to
>   '("a" "b" "c" "d")

(info "(Elisp) Building Lists")


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


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

* Re: Adding Lists/Sequences
  2008-09-23 11:52 ` Joost Kremers
@ 2008-09-23 13:22   ` Nordlöw
  0 siblings, 0 replies; 20+ messages in thread
From: Nordlöw @ 2008-09-23 13:22 UTC (permalink / raw)
  To: help-gnu-emacs

On 23 Sep, 13:52, Joost Kremers <joostkrem...@yahoo.com> wrote:
> Nordlöw wrote:
> > Is there a general function, say foo, that adds lists or, even better,
> > sequences together?
>
> > I want this
> >   (foo '("a" "b") '("c" "d"))
> > to evaluate to
> >   '("a" "b" "c" "d")
>
> (info "(Elisp) Building Lists")
>
> --
> Joost Kremers                                      joostkrem...@yahoo.com
> Selbst in die Unterwelt dringt durch Spalten Licht
> EN:SiS(9)

Great!

Thanks,
Nordlöw


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

* Re: Adding Lists/Sequences
  2008-09-23 11:46 Adding Lists/Sequences Nordlöw
  2008-09-23 11:52 ` Joost Kremers
@ 2008-09-23 15:06 ` Tassilo Horn
  2008-09-23 15:28 ` Thierry Volpiatto
       [not found] ` <mailman.19769.1222183171.18990.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 20+ messages in thread
From: Tassilo Horn @ 2008-09-23 15:06 UTC (permalink / raw)
  To: help-gnu-emacs

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

Hi!

> Is there a general function, say foo, that adds lists or, even better,
> sequences together?
>
> I want this
>   (foo '("a" "b") '("c" "d"))
> to evaluate to
>   '("a" "b" "c" "d")

(defalias 'foo 'append) ;-)

Bye,
Tassilo





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

* Re: Adding Lists/Sequences
  2008-09-23 11:46 Adding Lists/Sequences Nordlöw
  2008-09-23 11:52 ` Joost Kremers
  2008-09-23 15:06 ` Tassilo Horn
@ 2008-09-23 15:28 ` Thierry Volpiatto
       [not found] ` <mailman.19769.1222183171.18990.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-23 15:28 UTC (permalink / raw)
  To: Nordlöw; +Cc: help-gnu-emacs

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

> Is there a general function, say foo, that adds lists or, even better,
> sequences together?
>
> I want this
>   (foo '("a" "b") '("c" "d"))
> to evaluate to
>   '("a" "b" "c" "d")

,----
| ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
| ("a" "b" "c" "d" "e" "f")
`----

Note: `append' is not destructive

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: Adding Lists/Sequences
       [not found] ` <mailman.19769.1222183171.18990.help-gnu-emacs@gnu.org>
@ 2008-09-23 19:26   ` Pascal J. Bourguignon
  2008-09-23 20:48     ` Thierry Volpiatto
  2008-09-23 22:11   ` David Kastrup
  1 sibling, 1 reply; 20+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-23 19:26 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> Nordlöw <per.nordlow@gmail.com> writes:
>
>> Is there a general function, say foo, that adds lists or, even better,
>> sequences together?
>>
>> I want this
>>   (foo '("a" "b") '("c" "d"))
>> to evaluate to
>>   '("a" "b" "c" "d")
>
> ,----
> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
> | ("a" "b" "c" "d" "e" "f")
> `----

Very bad!

(defun bad-bad (tail)
   (nconc '(a b c) tail))

(bad-bad '(1 2 3)) --> (a b c 1 2 3)
(bad-bad '(4 5 6)) --> (a b c 1 2 3 4 5 6) ; !!!

NEVER use a destructive function on literal data!


> Note: `append' is not destructive

append is much better, but be careful that it shares the tail, so you
must consider the result as a literal data, unless you have consed the
tail yourself.


-- 
__Pascal_Bourguignon__


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

* Re: Adding Lists/Sequences
  2008-09-23 19:26   ` Pascal J. Bourguignon
@ 2008-09-23 20:48     ` Thierry Volpiatto
  0 siblings, 0 replies; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-23 20:48 UTC (permalink / raw)
  To: Pascal J. Bourguignon; +Cc: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>> Nordlöw <per.nordlow@gmail.com> writes:
>>
>>> Is there a general function, say foo, that adds lists or, even better,
>>> sequences together?
>>>
>>> I want this
>>>   (foo '("a" "b") '("c" "d"))
>>> to evaluate to
>>>   '("a" "b" "c" "d")
>>
>> ,----
>> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
>> | ("a" "b" "c" "d" "e" "f")
>> `----
>
> Very bad!
>
> (defun bad-bad (tail)
>    (nconc '(a b c) tail))
>
> (bad-bad '(1 2 3)) --> (a b c 1 2 3)
> (bad-bad '(4 5 6)) --> (a b c 1 2 3 4 5 6) ; !!!
>
> NEVER use a destructive function on literal data!

Good tip, Thank you.

>
>> Note: `append' is not destructive
>
> append is much better, but be careful that it shares the tail, so you
> must consider the result as a literal data, unless you have consed the
> tail yourself.

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: Adding Lists/Sequences
       [not found] ` <mailman.19769.1222183171.18990.help-gnu-emacs@gnu.org>
  2008-09-23 19:26   ` Pascal J. Bourguignon
@ 2008-09-23 22:11   ` David Kastrup
  2008-09-24  7:09     ` Thierry Volpiatto
       [not found]     ` <mailman.19799.1222239593.18990.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 20+ messages in thread
From: David Kastrup @ 2008-09-23 22:11 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> Nordlöw <per.nordlow@gmail.com> writes:
>
>> Is there a general function, say foo, that adds lists or, even better,
>> sequences together?
>>
>> I want this
>>   (foo '("a" "b") '("c" "d"))
>> to evaluate to
>>   '("a" "b" "c" "d")
>
> ,----
> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
> | ("a" "b" "c" "d" "e" "f")
> `----
>
> Note: `append' is not destructive

This is so bad that I can't believe it.

(defun ugh () (nconc '("a" "b" "c") '("d" "e" "f")))
(ugh) -> ("a" "b" "c" "d" "e" "f")
(ugh) -> ("a" "b" "c" "d" "e" "f" . #3)

The latter is a tail-cyclic list.

(ugh) -> hangs

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


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

* Re: Adding Lists/Sequences
  2008-09-23 22:11   ` David Kastrup
@ 2008-09-24  7:09     ` Thierry Volpiatto
  2008-09-24  9:28       ` Nikolaj Schumacher
       [not found]     ` <mailman.19799.1222239593.18990.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-24  7:09 UTC (permalink / raw)
  To: David Kastrup; +Cc: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>> Nordlöw <per.nordlow@gmail.com> writes:
>>
>>> Is there a general function, say foo, that adds lists or, even better,
>>> sequences together?
>>>
>>> I want this
>>>   (foo '("a" "b") '("c" "d"))
>>> to evaluate to
>>>   '("a" "b" "c" "d")
>>
>> ,----
>> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
>> | ("a" "b" "c" "d" "e" "f")
>> `----
>>
>> Note: `append' is not destructive
>
> This is so bad that I can't believe it.
>
> (defun ugh () (nconc '("a" "b" "c") '("d" "e" "f")))
> (ugh) -> ("a" "b" "c" "d" "e" "f")
> (ugh) -> ("a" "b" "c" "d" "e" "f" . #3)
>
> The latter is a tail-cyclic list.
>
> (ugh) -> hangs
We speak of what is bad and very bad but didn't answer to what is good
or not, i send here a bad exemple as i constat i never used that in my
programs (nconc), idem for `nreverse'.And when i read again my commonlisp
book it say that is very delicate to use destructives functions.
So can you explain when it is good (if it is) to use these destructive
functions.

Here an example of the use of append that is safe.


,----
| ELISP> (setq A '(a b c))
| (a b c)
| 
| ELISP> (setq B '(1 2 3))
| (1 2 3)
| 
| ELISP> (defun quite-good (beg tail)
|          (append beg tail))
| quite-good
| ELISP> (quite-good A B)
| (a b c 1 2 3)
| 
| ELISP> (setq C (quite-good A B))
| (a b c 1 2 3)
| 
| ELISP> A
| (a b c)
| 
| ELISP> B
| (1 2 3)
| 
| ELISP> C
| (a b c 1 2 3)
`----

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: Adding Lists/Sequences
  2008-09-24  7:09     ` Thierry Volpiatto
@ 2008-09-24  9:28       ` Nikolaj Schumacher
  2008-09-24  9:55         ` Thierry Volpiatto
  0 siblings, 1 reply; 20+ messages in thread
From: Nikolaj Schumacher @ 2008-09-24  9:28 UTC (permalink / raw)
  To: Thierry Volpiatto; +Cc: help-gnu-emacs, David Kastrup

Thierry Volpiatto <thierry.volpiatto@gmail.com> wrote:

> So can you explain when it is good (if it is) to use these destructive
> functions.

Generally, speed.  Non-destructive operations often create or copy a lot
of new cons cells.  So in time critical regions of code, when you're
only handling self-created data, and are very careful, that's the
advantage in using them.


regards,
Nikolaj Schumacher




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

* Re: Adding Lists/Sequences
  2008-09-24  9:28       ` Nikolaj Schumacher
@ 2008-09-24  9:55         ` Thierry Volpiatto
  2008-09-24 14:50           ` Drew Adams
  0 siblings, 1 reply; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-24  9:55 UTC (permalink / raw)
  To: Nikolaj Schumacher; +Cc: help-gnu-emacs, David Kastrup

Nikolaj Schumacher <me@nschum.de> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> wrote:
>
>> So can you explain when it is good (if it is) to use these destructive
>> functions.
>
> Generally, speed.  Non-destructive operations often create or copy a lot
> of new cons cells.  So in time critical regions of code, when you're
> only handling self-created data, and are very careful, that's the
> advantage in using them.
>
Thats clear, thank you.
-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* RE: Adding Lists/Sequences
  2008-09-24  9:55         ` Thierry Volpiatto
@ 2008-09-24 14:50           ` Drew Adams
  2008-09-24 15:31             ` Thierry Volpiatto
  0 siblings, 1 reply; 20+ messages in thread
From: Drew Adams @ 2008-09-24 14:50 UTC (permalink / raw)
  To: 'Thierry Volpiatto', 'Nikolaj Schumacher'
  Cc: help-gnu-emacs, 'David Kastrup'

>> explain when it is good (if it is) to use these destructive functions.
>
> Generally, speed.  Non-destructive operations often create 
> or copy a lot of new cons cells.

Or when you want to share Lisp structure. A special case is a circular
structure, which shares part(s) of itself with itself.





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

* Re: Adding Lists/Sequences
  2008-09-24 15:31             ` Thierry Volpiatto
@ 2008-09-24 15:28               ` David Kastrup
  2008-09-24 18:03                 ` Thierry Volpiatto
  2008-09-24 15:49               ` Drew Adams
  1 sibling, 1 reply; 20+ messages in thread
From: David Kastrup @ 2008-09-24 15:28 UTC (permalink / raw)
  To: Thierry Volpiatto; +Cc: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> "Drew Adams" <drew.adams@oracle.com> writes:
>
>>>> explain when it is good (if it is) to use these destructive functions.
>>>
>>> Generally, speed.  Non-destructive operations often create 
>>> or copy a lot of new cons cells.
>>
>> Or when you want to share Lisp structure. A special case is a circular
>> structure, which shares part(s) of itself with itself.
>>
> Can you provide a small example ?

(let ((x (list 1 2 3))) (nconc x x))

-- 
David Kastrup




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

* Re: Adding Lists/Sequences
  2008-09-24 14:50           ` Drew Adams
@ 2008-09-24 15:31             ` Thierry Volpiatto
  2008-09-24 15:28               ` David Kastrup
  2008-09-24 15:49               ` Drew Adams
  0 siblings, 2 replies; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-24 15:31 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs, 'David Kastrup'

"Drew Adams" <drew.adams@oracle.com> writes:

>>> explain when it is good (if it is) to use these destructive functions.
>>
>> Generally, speed.  Non-destructive operations often create 
>> or copy a lot of new cons cells.
>
> Or when you want to share Lisp structure. A special case is a circular
> structure, which shares part(s) of itself with itself.
>
Can you provide a small example ? 
-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* RE: Adding Lists/Sequences
  2008-09-24 15:31             ` Thierry Volpiatto
  2008-09-24 15:28               ` David Kastrup
@ 2008-09-24 15:49               ` Drew Adams
  2008-09-24 17:59                 ` Thierry Volpiatto
  1 sibling, 1 reply; 20+ messages in thread
From: Drew Adams @ 2008-09-24 15:49 UTC (permalink / raw)
  To: 'Thierry Volpiatto'; +Cc: help-gnu-emacs, 'David Kastrup'

> >>> explain when it is good (if it is) to use these 
> >>> destructive functions.
> >>
> >> Generally, speed.  Non-destructive operations often create 
> >> or copy a lot of new cons cells.
> >
> > Or when you want to share Lisp structure. A special case is 
> > a circular structure, which shares part(s) of itself with itself.
>
> Can you provide a small example ? 

(I meant "list structure", not "Lisp structure" BTW.)

Real examples are best for this kind of thing. Grep for any of these destructive
functions in the Emacs installation `lisp' directory and its subdirectories:
`nconc', `setcdr', `setcar', `delq', `(delete' (you'll get too many hits for
just `delete'), `nreverse', or `sort' . 

You will see in the code that such functions are used _judiciously_, never
wantonly, and you will likely be able to see why, in context. Sometimes comments
explain the need for structure modification or sharing in a particular context.
If you don't understand the existing code, however, then I'd say stay away from
using list modification in your own code. 

Also, see node `Modifying Lists' and its subnodes in the Elisp manual. They give
how-to examples and explain the use. Likewise, see node `Circular Objects'.

If you really need to modify or share list structure for some reason, you will
probably know about it. ;-) IOW, a real need will lead you to finding out more
about it. If not, it's a good idea to stay away from using it. 

And, other things being equal, it's generally a bad idea to try using
modification just to speed up your code. IOW, don't try substituting `nconc' for
`append' liberally, thinking that you are optimizing things. It is more likely
that you are breaking things. (Of course, breaking things can sometimes be a
good way to learn, but beware that debugging code that is broken because of list
modification can be confusing.)






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

* Re: Adding Lists/Sequences
  2008-09-24 15:49               ` Drew Adams
@ 2008-09-24 17:59                 ` Thierry Volpiatto
  0 siblings, 0 replies; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-24 17:59 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs, 'David Kastrup'

"Drew Adams" <drew.adams@oracle.com> writes:

>> >>> explain when it is good (if it is) to use these 
>> >>> destructive functions.
>> >>
>> >> Generally, speed.  Non-destructive operations often create 
>> >> or copy a lot of new cons cells.
>> >
>> > Or when you want to share Lisp structure. A special case is 
>> > a circular structure, which shares part(s) of itself with itself.
>>
>> Can you provide a small example ? 
>
> (I meant "list structure", not "Lisp structure" BTW.)
>
> Real examples are best for this kind of thing. Grep for any of these destructive
> functions in the Emacs installation `lisp' directory and its subdirectories:
> `nconc', `setcdr', `setcar', `delq', `(delete' (you'll get too many hits for
> just `delete'), `nreverse', or `sort' . 
>
> You will see in the code that such functions are used _judiciously_, never
> wantonly, and you will likely be able to see why, in context. Sometimes comments
> explain the need for structure modification or sharing in a particular context.
> If you don't understand the existing code, however, then I'd say stay away from
> using list modification in your own code. 
>
> Also, see node `Modifying Lists' and its subnodes in the Elisp manual. They give
> how-to examples and explain the use. Likewise, see node `Circular Objects'.
>
> If you really need to modify or share list structure for some reason, you will
> probably know about it. ;-) IOW, a real need will lead you to finding out more
> about it. If not, it's a good idea to stay away from using it. 
>
> And, other things being equal, it's generally a bad idea to try using
> modification just to speed up your code. IOW, don't try substituting `nconc' for
> `append' liberally, thinking that you are optimizing things. It is more likely
> that you are breaking things. (Of course, breaking things can sometimes be a
> good way to learn, but beware that debugging code that is broken because of list
> modification can be confusing.)
>
>
Thank you a lot for this, that's very instructive.
I already "grep" a lot source code in emacs to learn more, your code too ;)
-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: Adding Lists/Sequences
  2008-09-24 15:28               ` David Kastrup
@ 2008-09-24 18:03                 ` Thierry Volpiatto
  0 siblings, 0 replies; 20+ messages in thread
From: Thierry Volpiatto @ 2008-09-24 18:03 UTC (permalink / raw)
  To: David Kastrup; +Cc: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>
>> "Drew Adams" <drew.adams@oracle.com> writes:
>>
>>>>> explain when it is good (if it is) to use these destructive functions.
>>>>
>>>> Generally, speed.  Non-destructive operations often create 
>>>> or copy a lot of new cons cells.
>>>
>>> Or when you want to share Lisp structure. A special case is a circular
>>> structure, which shares part(s) of itself with itself.
>>>
>> Can you provide a small example ?
>
> (let ((x (list 1 2 3))) (nconc x x))

Ok, thanks for this example.
-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: Adding Lists/Sequences
       [not found]     ` <mailman.19799.1222239593.18990.help-gnu-emacs@gnu.org>
@ 2008-09-25  5:27       ` Tim X
  2008-09-25 20:34         ` David Kastrup
  0 siblings, 1 reply; 20+ messages in thread
From: Tim X @ 2008-09-25  5:27 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> David Kastrup <dak@gnu.org> writes:
>
>> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
>>
>>> Nordlöw <per.nordlow@gmail.com> writes:
>>>
>>>> Is there a general function, say foo, that adds lists or, even better,
>>>> sequences together?
>>>>
>>>> I want this
>>>>   (foo '("a" "b") '("c" "d"))
>>>> to evaluate to
>>>>   '("a" "b" "c" "d")
>>>
>>> ,----
>>> | ELISP> (nconc '("a" "b" "c") '("d" "e" "f")) 
>>> | ("a" "b" "c" "d" "e" "f")
>>> `----
>>>
>>> Note: `append' is not destructive
>>
>> This is so bad that I can't believe it.
>>
>> (defun ugh () (nconc '("a" "b" "c") '("d" "e" "f")))
>> (ugh) -> ("a" "b" "c" "d" "e" "f")
>> (ugh) -> ("a" "b" "c" "d" "e" "f" . #3)
>>
>> The latter is a tail-cyclic list.
>>
>> (ugh) -> hangs
> We speak of what is bad and very bad but didn't answer to what is good
> or not, i send here a bad exemple as i constat i never used that in my
> programs (nconc), idem for `nreverse'.And when i read again my commonlisp
> book it say that is very delicate to use destructives functions.
> So can you explain when it is good (if it is) to use these destructive
> functions.
>
> Here an example of the use of append that is safe.
>
>
> ,----
> | ELISP> (setq A '(a b c))
> | (a b c)
> | 
> | ELISP> (setq B '(1 2 3))
> | (1 2 3)
> | 
> | ELISP> (defun quite-good (beg tail)
> |          (append beg tail))
> | quite-good
> | ELISP> (quite-good A B)
> | (a b c 1 2 3)
> | 
> | ELISP> (setq C (quite-good A B))
> | (a b c 1 2 3)
> | 
> | ELISP> A
> | (a b c)
> | 
> | ELISP> B
> | (1 2 3)
> | 
> | ELISP> C
> | (a b c 1 2 3)
> `----

I think there are possibly two different issues here that need to be
considered. 

1. Modifying 'literal' lists is usually risky. Weird things can happen
because on some levels, you are modifying what is best considered a
constant. This is the difference between '(1 2 3) and (list 1 2 3). 

2. The destructive operators manipulate the structure of the list rather
than creating a new copy with its own structure. This can have two
problems. Firstly, you can create circular lists as David points out and
more generally, you can get unexpected 'side effects' if that structure is
also bound to another symbol. In general, I think yo should only use the
destructive operators on a structure which you know is not shared. For
example, you could define your function so that it makes a copy of the
list and then applies a distructive operation to the list and return the
new list rather than just applying the destructive operation to the list
that is passed in. Of course, the problem with tis is that often the
destructive functions are used for performance reasons and having to
make a copy each time may cause its own performance problems.

HTH

Tim


-- 
tcross (at) rapttech dot com dot au


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

* Re: Adding Lists/Sequences
  2008-09-25  5:27       ` Tim X
@ 2008-09-25 20:34         ` David Kastrup
  2008-09-25 22:16           ` Pascal J. Bourguignon
  0 siblings, 1 reply; 20+ messages in thread
From: David Kastrup @ 2008-09-25 20:34 UTC (permalink / raw)
  To: help-gnu-emacs

Tim X <timx@nospam.dev.null> writes:

> 1. Modifying 'literal' lists is usually risky. Weird things can happen
> because on some levels, you are modifying what is best considered a
> constant.

That's all very vague and fuzzy, but there are no vague or fuzzy
semantics involved.

> This is the difference between '(1 2 3) and (list 1 2 3).

The difference is that the first form is turned into a list by the Lisp
reader, each time it is passed through the reader (in most cases, once).
In interactive use, it is perfectly equivalent to the second form.  When
put into function definitions or bound to anything, it isn't.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


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

* Re: Adding Lists/Sequences
  2008-09-25 20:34         ` David Kastrup
@ 2008-09-25 22:16           ` Pascal J. Bourguignon
  0 siblings, 0 replies; 20+ messages in thread
From: Pascal J. Bourguignon @ 2008-09-25 22:16 UTC (permalink / raw)
  To: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

> Tim X <timx@nospam.dev.null> writes:
>
>> 1. Modifying 'literal' lists is usually risky. Weird things can happen
>> because on some levels, you are modifying what is best considered a
>> constant.
>
> That's all very vague and fuzzy, but there are no vague or fuzzy
> semantics involved.

Yes, there is vague and fuzzy semantics involved.

The vagueness and fuzzyness is not very well characterised, because
emacs lisp is not defined by a "standard", only by a (couple of)
implementation(s). 

Nonetheless, nowhere can you see documented what the compiler does
with literal objects.  It could merge or duplicate them at will.
Therefore self modifying code won't behave the same when interpreted
and when compiled.


>> This is the difference between '(1 2 3) and (list 1 2 3).
>
> The difference is that the first form is turned into a list by the Lisp
> reader, each time it is passed through the reader (in most cases, once).
> In interactive use, it is perfectly equivalent to the second form.  When
> put into function definitions or bound to anything, it isn't.

Yes, another fuzzyness.

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

HANDLE WITH EXTREME CARE: This product contains minute electrically
charged particles moving at velocities in excess of five hundred
million miles per hour.


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

end of thread, other threads:[~2008-09-25 22:16 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-23 11:46 Adding Lists/Sequences Nordlöw
2008-09-23 11:52 ` Joost Kremers
2008-09-23 13:22   ` Nordlöw
2008-09-23 15:06 ` Tassilo Horn
2008-09-23 15:28 ` Thierry Volpiatto
     [not found] ` <mailman.19769.1222183171.18990.help-gnu-emacs@gnu.org>
2008-09-23 19:26   ` Pascal J. Bourguignon
2008-09-23 20:48     ` Thierry Volpiatto
2008-09-23 22:11   ` David Kastrup
2008-09-24  7:09     ` Thierry Volpiatto
2008-09-24  9:28       ` Nikolaj Schumacher
2008-09-24  9:55         ` Thierry Volpiatto
2008-09-24 14:50           ` Drew Adams
2008-09-24 15:31             ` Thierry Volpiatto
2008-09-24 15:28               ` David Kastrup
2008-09-24 18:03                 ` Thierry Volpiatto
2008-09-24 15:49               ` Drew Adams
2008-09-24 17:59                 ` Thierry Volpiatto
     [not found]     ` <mailman.19799.1222239593.18990.help-gnu-emacs@gnu.org>
2008-09-25  5:27       ` Tim X
2008-09-25 20:34         ` David Kastrup
2008-09-25 22:16           ` Pascal J. Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.