Some more GAP objects
OverviewTeaching: 15 min
Exercises: 5 minQuestions
Further examples of objects and operations with themObjectives
See examples of types that are built-in in GAP but may be missing in other systems
See examples of list arithmetic
So far we have met three types of GAP types:
simple objects such as integers, rationals, booleans, permutations;
composite objects such as lists;
objects with more complex internal representation, such as groups.
In this section, we will demonstrate some other examples of basic objects that exist in GAP (the system is extendable, so one can introduce new types of objects, but this is beyond the scope of this lesson!).
Some other simple objects are floats, cyclotomics and finite field elements:
E(4); E(4)^2; E(6);
E(4) -1 -E(3)^2
AsList(GF(2)); Z(5); Z(5)^4;
[ 0*Z(2), Z(2)^0 ] Z(5) Z(5)^0
You already know about lists.
Another type of composite objects is records. While a list contains subobjects indexed
by their positions in the list, a record contains subobjects, called record
components, which are indexed by their names. Elements of a record are accessed with
date:= rec(year:= 2015, month:= "Nov", day:= 17);
rec( day := 17, month := "Nov", year := 2015 )
date.time:= rec(hour:= 14, minute:= 55, second:= 12);
rec( hour := 14, minute := 55, second := 12 )
rec( day := 17, month := "Nov", time := rec( hour := 14, minute := 55, second := 12 ), year := 2015 )
[ "time", "year", "month", "day" ]
Next, there are strings and characters. While strings are printed specially by GAP, a string is really just a list of characters, and any function which takes a list will also take a string. In contrast, characters are simple objects like integers.
gap> w:="supercalifragilisticexpialidocious"; Length(w);
Strings are denoted by double quotes, and characters by single ones.
gap> "s" in w; 's' in w; IsSubset(w,"s"); IsSubset(w,['s','f']); ['c','a','t'] = "cat";
false true true true true
gap> PositionSublist(w,"sf"); PositionSublist(w,"fr");
Be careful! Some operations may create a new list, while others are destructive. For example:
gap> SortedList(w); w;
gap> Sort(w); w;
Which letter occurs in “supercalifragilisticexpialidocious” most often?
gap> c := Collected(w);
[ [ 'a', 3 ], [ 'c', 3 ], [ 'd', 1 ], [ 'e', 2 ], [ 'f', 1 ], [ 'g', 1 ], [ 'i', 7 ], [ 'l', 3 ], [ 'o', 2 ], [ 'p', 2 ], [ 'r', 2 ], [ 's', 3 ], [ 't', 1 ], [ 'u', 2 ], [ 'x', 1 ] ]
gap> k := Maximum( List( c, v -> v ) ); Filtered( c, v -> v = 7 );
7 [ [ 'i', 7 ] ]
Finding the most common letter(s) in a list using only one pass
k := Maximum( List( c, v -> v ) ); Filtered( c, v -> v = 7 );
iterates over the list
Filtered), and it also iterates over another list of the same length as
cin the call to
Maximum. If the list is long, this will impose certain performance and memory penalties. Try to write code that finds the letters that occur most in
cwithout producing an intermediate list.
GAP has a plethora of various immediate, positional and component objects.
List arithmetic is very flexible and powerful.
Objects like lists and records are good to keep structured and related data.