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

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

Re: using setq to create lists based on other lists...


From: Jean-Christophe Helary
Subject: Re: using setq to create lists based on other lists...
Date: Sun, 2 Dec 2018 21:03:04 +0900

Barry, thank you for the reply.

> On Dec 2, 2018, at 20:21, Barry Margolin <barmar@alum.mit.edu> wrote:
> 
>> I spend most of the day investigating why creating a list with setq was not 
>> "working".
>> 
>> For ex:
>> (setq list0 '(1 2))
>> (setq list1 list0)
>> 
>> If you do
>> 
>> (setcar list0 0)
>> 
>> then for some reason (for which I could not find an explanation in the elisp 
>> reference) the car of list1 also changes, and vice-versa.
>> 
>> Which is totally unexpected since when you do:
>> 
>> (setq list0 0)
>> 
>> list1 does not become 0
>> 
>> I don't suppose that's a bug, but really it ought the be very clearly 
>> documented in the reference. Also, I'd like to know why that's happening.
> 
> list0 and list1 both contain references to the same cons.

The reference says:

> Note that the first form is evaluated, then the first symbol is set...

So in my case I expected to have list0 evaluated to (1 2) and list1 set to (1 
2).

You mean that list0 evaluates to a pointer to a cons that holds the values (1 
2) and that list1 is set to that pointer.

I'm ok if that is true but I wish it were plainly written in the reference. 
Because I can't find a place where it is clearly written that lists evaluate to 
a pointer to their cons.

> If you're familiar with C, you can think of Lisp variables as being like 
> pointers, and conses are like structures. Your code is equivalent to:

I am not familiar with C but think you for the explanations.

> cons *list0 = malloc(sizeof(cons));
> list0->car = make_number(1);
> list0->cdr = malloc(sizeof(cons));
> list0->cdr->car = make_number(2);
> list0->cdr->cdr = null;
> cons *list1 = list0;
> list0->car = make_number(0);


Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune





reply via email to

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