GAP Lesson 2


Change to the directory where you store your GAP files, and then start up GAP. In the session which follows, pause for discussion at the spaces between command groupings.

gap> LogTo("GAPlesson2");

Lists
You learn: [] notation for lists, how to access terms in a list, how lists are stored and duplicated, Sort, Add, Append, Length, Position, Flat, vector operations, List, ranges.

gap> Primes;
gap> Primes[5];
gap> 15 in Primes;
gap> Position(Primes,31);
gap> Length(Primes);

gap> things:=["apple",true,,17];
gap> things[1];
gap> things[3];
gap> things[4]:=9;
gap> things[6]:=[1,2,3];
gap> things;

gap> newthings[1]:=72;
gap> newthings;
gap> newthings:=[];
gap> newthings;
gap> newthings[1]:=72;
gap> newthings;

gap> newthings:=things;
gap> newthings[1]:=23;
gap> newthings;
gap> things;
gap> things[1]:=14;
gap> things;
gap> newthings;

gap> newthings:=ShallowCopy(things);
gap> newthings[1]:=23;
gap> newthings;
gap> things;

gap> Add(things,16);
gap> things;
gap> Add(things,newthings);
gap> things;
gap> Append(things,newthings);
gap> things;
gap> Flat(things);

gap> ?Lists
gap> ?Flat


gap> r:=[1,2,3];
gap> s:=[3,1,4];
gap> r+s;
gap> 2*r;
gap> Sort(s);
gap> s;

gap> Append(r,s);
gap> r;
gap> List(r,x->x^2);
gap> List([1..10],IsPrime);
gap> [1..10];
gap> List([1..10],x->x);

gap> g:=Group((1,2,3),(1,2));
gap> els:=Elements(g);
gap> List(els,x->Order(x));

gap> els[3]:=14;
gap> els:=ShallowCopy(els);
gap> els[3]:=14;
gap> els;


Programming

You learn: do loops, how to initialize a list.

gap> g:=Group((1,2,3),(1,2));
gap> els:=Elements(g);

Discuss what would happen if you were to do the following (don't try it unless you want to):

gap> for i in els do
> Order(i);
> od;

Instead, do this:

gap> orders:=[];
gap> for i in els do
> Add(orders,Order(i));
> od;
gap> orders;

The following is perhaps a less elegant way to program a list of orders of the elements of g because the enumeration is done over [1..Length(els)] instead of els itself. However, sometimes we have to do this kind of inelegant thing.

gap> orders:=[];
[ ]
gap> for i in [1..Length(els)] do
> orders[i]:=Order(els[i]);
> od;
gap> orders;
[ 1, 2, 2, 3, 3, 2 ]

At this point we note that there are other forms of do loop, namely
while ... do ... od;
and
repeat... until...;

Note the following:

gap> for i in [1..10] do
> Print(i);
> od;

gap> for i in [1..10] do
> Print(i,"\n");
> od;

Reading a file of code into GAP
On http://www.math.umn.edu/~webb there is a file called Conway. Create a copy of this file in your GAP file directory. Its contents are:

Conway:=function(seq)
local i, r, newseq;
newseq:=[];
i:=1;
while i<=Length(seq) do
r:=0;
repeat r:=r+1;
until i+r>Length(seq) or not seq[i]=seq[i+r];
Append(newseq,[r,seq[i]]);
i:=i+r;
od;
return(newseq);
end;

Next, do

gap> Read("Conway");
gap> Print(Conway);

gap> Conway([2]);
gap> Conway([1,2]);
gap> Conway(last);
gap> Conway(last);
gap> Conway(last);