difference and intersect

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

difference and intersect

Post by newdep »

Hi Lutz,

Perhpas you have a hint?

Is there way to create a somekind of "intelligent" addon for difference and intersect?

Currently these functions always handle from List-A to List-B but the function is
not smart enough the detect if List-A is i.e. bigger then List-B or even that List-A
has more symbols then List-B and thus should compare to List-B instead of List-A.

Secondly, Is there a way/function to automaticly select between 'intersect and 'difference.
Sometimes comparing lists 'difference should be used and sometimes 'intersect.
But this depends on the structure of the lists and is not easy to build a function around,
This is also linked to the problem above.

Regards, Norman.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

This could lead to ambigous situations:

Code: Select all

> (set 'A '(a b c d e f g) 'B '(c d e) 'C '(a b c e f g h))
(a b c e f g h)
> (difference C A)
(h)
> (difference A C)
(d)
> 
... it would only work for lists A and B

Code: Select all

> (difference A B)
(a b f g)
> (difference B A)
()
> 
... in that case you could do:

Code: Select all

> (or (difference A B) (difference B A))
(a b f g)
Lutz

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

It seems to be quite clear from the manual that 'difference' and 'intersect' conform to their usual set theoretic definitions (notions). Note also that 'difference' is not a commutative operation, but 'intersect' is.

As an aside, with 'difference', we can define the subset relation as the empty difference relation (with the help of our composer, discussed in another thread).

Code: Select all

(define subset? (compose empty? difference))
Better yet, why not fold the empty difference over (possibly) more than 2 sets?

Code: Select all

(define subset? (curry foldr (compose empty? difference) empty-set))
('foldr' is a right associative reducer, 'curry' is the partial call functor, and 'empty-set' should be '() in our case.)

Then you can see if A ⊆ B ⊆ C by saying

Code: Select all

(subset? A B C)
I haven't tested it, but you get my drift.

And the superset relation is the inverse of the subset.

Code: Select all

(define (superset?) (apply subset? (reverse (args))))
Does anyone already have a set library? Curious, --Rick
(λx. x x) (λx. x x)

Locked