Re: Destructive side effect of the union operator
Bruno Emond <brunoemond@fastmail.fm> writes:
> Interesting destructive side effect of the union operator.
> It actually only shows after a sort is done on the union.
> Bruno
>
> CL-USER 125 : 3 > (setf x '(1 2 3 4))
> (1 2 3 4)
>
> CL-USER 126 : 3 > (setf y '(5 6 7 8))
> (5 6 7 8)
>
> CL-USER 127 : 3 > (setf w (append x y))
> (1 2 3 4 5 6 7 8)
>
> CL-USER 128 : 3 > (setf z (union x y))
> (8 7 6 5 1 2 3 4)
>
> CL-USER 129 : 3 > x
> (1 2 3 4)
So far, so good. Conforming code above.
> CL-USER 130 : 3 > (sort w #'>)
> (8 7 6 5 4 3 2 1)
This call to SORT is not conforming, because W contains literal data.
UNION is NOT DESTRUCTIVE.
NUNION is NON-consing, and therefore MAY HAVE TO BE destructive.
Compare:
[pjb@kuiper :0.0 ~]$ clall -r '(let ((a (list 1 3 5)) (b (list 2 4 6))) (values (NUNION a b) a b))'
Armed Bear Common Lisp --> (5 3 1 2 4 6), (1 2 4 6), (2 4 6)
Clozure Common Lisp --> (5 3 1 2 4 6), (1 2 4 6), (2 4 6)
CLISP --> (1 3 5 2 4 6), (1 3 5 2 4 6), (2 4 6)
CMU Common Lisp --> (5 3 1 2 4 6), (1 2 4 6), (2 4 6)
ECL --> (1 3 5 2 4 6), (1 3 5 2 4 6), (2 4 6)
SBCL --> (5 3 1 2 4 6), (1 2 4 6), (2 4 6)
[pjb@kuiper :0.0 ~]$ clall -r '(let ((a (list 1 3 5)) (b (list 2 4 6))) (values (UNION a b) a b))'
Armed Bear Common Lisp --> (5 3 1 2 4 6), (1 3 5), (2 4 6)
Clozure Common Lisp --> (5 3 1 2 4 6), (1 3 5), (2 4 6)
CLISP --> (1 3 5 2 4 6), (1 3 5), (2 4 6)
CMU Common Lisp --> (5 3 1 2 4 6), (1 3 5), (2 4 6)
ECL --> (1 3 5 2 4 6), (1 3 5), (2 4 6)
SBCL --> (5 3 1 2 4 6), (1 3 5), (2 4 6)
Of course, if NUNION doesn't have to be destructive, then it is not
(there's no point in doing useless work):
[pjb@kuiper :0.0 ~]$ clall -r '(setf *print-circle* t)' '(let ((a (list 1 3 5)) (b (list 1 3 5))) (list (nunion a b) a b))'
Armed Bear Common Lisp --> T
Armed Bear Common Lisp --> (#1=(1 3 5) (1 3 5) #1#)
Clozure Common Lisp --> T
Clozure Common Lisp --> (#1=(1 3 5) (1 3 5) #1#)
CLISP --> T
CLISP --> (#1=(1 3 5) (1 3 5) #1#)
CMU Common Lisp --> T
CMU Common Lisp --> (#1=(1 3 5) (1 3 5) #1#)
ECL --> T
ECL --> (#1=(1 3 5) (1 3 5) #1#)
SBCL --> T
SBCL --> (#1=(1 3 5) (1 3 5) #1#)
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"
_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html