# 45.6 In for Records

element in rec

Usually the membership test is only defined for lists. However a record may contain a special operations record, that contains a function that is called when this record is the right operand of the in operator. The precise mechanism is as follows.

If the right operand of the in operator is a record, and if this record contains an element with the name operations that is a record, and if this record in turn contains an element with the name in that is a function, then this function is called with the two operands as arguments, and the value of the membership test is the value returned by that function. The function should of course return true or false.

The following example shows one piece of the definition of residue classes, using record operations. Of course this is far from a complete implementation (see About Defining New Group Elements). Note that the in must be quoted, so that it is taken as an identifier (see Identifiers).

    gap> ResidueClassOps := rec( );;
gap> ResidueClassOps.\in := function ( l, r )
>   if IsInt( l )  then
>     return (l - r.representative) mod r.modulus = 0;
>   else
>     return false;
>   fi;
> end;;
gap> ResidueClass := function ( representative, modulus )
>   return rec(
>     representative := representative,
>     modulus        := modulus,
>     operations     := ResidueClassOps );
> end;;
gap> l := ResidueClass( 13, 23 );;
gap> -10 in l;
true
gap> 10 in l;
false 

GAP 3.4.4
April 1997