Page 1 of 1

eq eql at work...

Posted: Mon Oct 15, 2012 7:39 am
by megera
HI
I have a query about that functions..
for ex. if I build a list:
(setf x ' (a b c))
and after I set : (setf y x)
now x and y are two different pointers to the same object in memory (like in Python's behavior...), is it?
infact : (eq x y) -> TRUE like (eql x y) t...(ps I know that two objects are eq if they are actually the same object in memory with same value;
but if i use push. : (push 'd x), only x changes not y!...then I can suppose that it's not a change_in_place...but a new list is create!!..right??

But if after use pop : (pop x) -> x returns now to have the same value of y but I suppose that now they are different object in memory, because also pop create a new list:

Code: Select all

(let ((x (car 1st)))
   (setf 1st (cdr 1st))
   x)
but if now i try to compare the two list : (eq x y) I would expect NIL but T happens...then NOW I'm confusing about eq & eql...
sorry for my bad english and thanks in advance for help !!!

Re: eq eql at work...

Posted: Mon Oct 15, 2012 11:39 am
by sylwester
push and pop are actually macroes:

Code: Select all

(macroexpand-1 '(push 'd x)) ==>
(SETQ X (CONS 'D X)) ;

(macroexpand '(pop x)) ==>
(LET ((TMP-1 (CAR X))) 
    (SETQ X (CDR X)) 
    TMP-1) 
As you suspected CL doesn't change the target but makes a new list and associate X with that instead.
Y still points to the same value that X once pointed to and that is still a part of X.
Thus this is eq:

Code: Select all

(eq (cdr x) y) ==>
T
pop associated x with (cdr x) and thus it again points to the same location in memory as y do.

Re: eq eql at work...

Posted: Mon Oct 15, 2012 12:24 pm
by megera
Thanks sylwester !!
now it's clear :!: