newLISP Fan Club

Forum => newLISP in the real world => Topic started by: newdep on November 16, 2006, 12:15:48 AM

Title: difference and intersect
Post by: newdep on November 16, 2006, 12:15:48 AM
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.
Title:
Post by: Lutz on November 16, 2006, 04:53:40 AM
This could lead to ambigous situations:



> (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


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


... in that case you could do:


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


Lutz
Title:
Post by: rickyboy on November 16, 2006, 10:03:46 AM
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).


(define subset? (compose empty? difference))

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


(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


(subset? A B C)

I haven't tested it, but you get my drift.



And the superset relation is the inverse of the subset.


(define (superset?) (apply subset? (reverse (args))))

Does anyone already have a set library?  Curious, --Rick