[GAP Forum] Find Intersection of Sequence

Thomas Breuer sam at Math.RWTH-Aachen.De
Sun Jun 22 21:03:28 BST 2014


Dear Forum,

Siddiqua Mazhar wrote:

> If we have a list like [[2,1],[5,2,1],[6,5,2,1],[9,10]] and I want to find the result in the form of [6,5,2,1].[9,10] and want to delete all those list that are contatined in the other. How may I progrme this
> 
> I do like let FV:= [[2,1],[5,2,1],[6,5,2,1],[9,10]];
> 
> Intersection:=function(FV)
> local A,B,a;
> for A in FV do
>   for B in FV do
>   if Intersection(A,B)=B then
>   a:=Position(FV,B);
>   Unbind(FV[a]);
>   fi;
>   od;
> od;
> 
> return FV;
> end;
> 
> However, this program gives me empty list. Could you please guide me in this program.

The first problem is that the function should not be called
'Intersection'; note that this would overwrite the GAP function
with this name, and then one would not be able to call this
function inside.
In fact, trying to read the above code into GAP would cause
an error message because GAP's 'Intersection' is write protected.

The second problem is that GAP's 'Intersection', when called with
two lists, returns the *sorted* list of all those elements that
appear in these two lists.
Therefore, the condition 'Intersection( A, B ) = B' is *not*
satisfied if 'B' is not sorted, even if all entries of 'B' appear in 'A'.
(In particular, it is not true that the above function will return
the empty list when it is called with the above list 'FV'.)

The third problem is that if the condition would be replaced for 
example by 'Intersection( A, B ) = Set( B )' then the return value
would really be an empty list, because the two for loops compare
among others the intersection of 'A' and 'A' with 'Set( A )',
and then 'A' gets kicked out from the list.

(Perhaps the fourth problem is that the argument FV gets changed
in place, which is probably not intended.  If this behaviour is
intended then there is no need to return a result.)

All the best,
Thomas




More information about the Forum mailing list