[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Wierd behaviour in using union on 2 lists

Anoop GR

9/12/2015 5:12:00 PM

;; I am using sbcl

(union '(a a b) '(c d a))
;; returns '(B C D A)

it seems as if elements that repeat are not in the union
however,

(union '(a a b) '(b b b))
;; returns '(A A B B B)

here elements repeat in the union

How is union defined in common lisp?
When are repeat elements included in the union of 2 lists?
8 Answers

Jocelyn Fréchot

9/12/2015 5:51:00 PM

0

On 12/09/2015 19:11, Anoop GR wrote:

> How is union defined in common lisp?

You should refer to the spec:

http://www.lispworks.com/documentation/HyperSpec/Body/f_...


> When are repeat elements included in the union of 2 lists?

â??If there is a duplication between list-1 and list-2, only one of
the duplicate instances will be in the result. If either list-1 or
list-2 has duplicate entries within it, the redundant entries might or
might not appear in the result.�

--
Jocelyn Fréchot

Barry Margolin

9/12/2015 6:04:00 PM

0

In article <55f4661e$0$3043$426a34cc@news.free.fr>,
Jocelyn Fréchot <jocelyn_frechot@yahoo.fr> wrote:

> On 12/09/2015 19:11, Anoop GR wrote:
>
> > How is union defined in common lisp?
>
> You should refer to the spec:
>
> http://www.lispworks.com/documentation/HyperSpec/Body/f_...
>
>
> > When are repeat elements included in the union of 2 lists?
>
> â??If there is a duplication between list-1 and list-2, only one of
> the duplicate instances will be in the result. If either list-1 or
> list-2 has duplicate entries within it, the redundant entries might or
> might not appear in the result.�

Basically, UNION only works predictably if the arguments are "sets", not
arbitrary lists. Sets, by definition, don't contain duplicates.

This is true for all the functions that treat lists as sets.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

rpw3

9/12/2015 6:12:00 PM

0

Anoop GR <anoopemacs@gmail.com> wrote:
+---------------
| How is union defined in common lisp?
| When are repeat elements included in the union of 2 lists?
+---------------

For these sorts of questions, it's best to refer to the spec itself:

http://www.lispworks.com/documentation/HyperSpec/Body/f_...
...
If there is a duplication between list-1 and list-2,
only one of the duplicate instances will be in the result.
If either list-1 or list-2 has duplicate entries within it,
the redundant entries might or might not appear in the result.

The order of elements in the result do not have to reflect the
ordering of list-1 or list-2 in any way.
...

To say it another way, you can only be sure that there will
be no duplicates in the result if neither of the input lists
contains duplicates within them. [Duplicates *between* the two
input lists is o.k.]

That is, if unwanted duplicates in the inputs are possible,
then instead of (UNION list-1 list-2) you'll need to do
(UNION (REMOVE-DUPLICATES list-1) (REMOVE-DUPLICATES list-2)).


-Rob

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

Anoop GR

9/12/2015 9:41:00 PM

0

Thank you very much Barry,
Treating lists as sets is something I could not find in the lispworks documentation
Your answer made it clear to me

Anoop GR

9/12/2015 9:43:00 PM

0

Thank you very much Barry
Your answer had more clarity than the lispworks documentation

Anoop GR

9/12/2015 9:46:00 PM

0

Thank you Jocelyn and Rob

Barry Margolin

9/12/2015 11:59:00 PM

0

In article <a545061a-868e-493b-b592-f0542461bd25@googlegroups.com>,
Anoop GR <anoopemacs@gmail.com> wrote:

> Thank you very much Barry,
> Treating lists as sets is something I could not find in the lispworks
> documentation
> Your answer made it clear to me

It wasn't obvious from the name UNION? UNION and INTERSECTION are
standard operations on sets.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Anoop GR

9/13/2015 7:35:00 AM

0

I wrongly felt that lisp had a union that works on lists that is different from the mathematics definition on sets
I did not actually know that sets are defined as those w/o repeat elements
Mathematics noob here :)