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[29];
gap> Position(Primes,29);
gap> 15 in Primes;
gap> Length(Primes);

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

Discuss. What restrictions are there on the data types of the entries in a list? How many data types can you think of that GAP knows?

gap> newthings[1]:=72;
gap> newthings;
gap> newthings:=[];
gap> newthings;
gap> newthings[4]:=9;
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;

Discuss. Quite a few things went on since we last discussed. Summarize what you have to do to set up a list, and what doesn't work. What strange behavior happened when we assigned things to be newthings? If you were designing a computer package like GAP, would you have set it up so as to have this behavior? How do we get round this strange behavior?

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

Was there any functional difference between the effects of Add, Append and Flat? Is there any grammatical difference between these words? What should Flat have been called if it worked the same way as Add and Append?

gap> r:=[3,1,4,1];
gap> 2*r;
gap> r+[,2,3];
gap> r+1;
gap> List(r,x->x^2);
gap> r;
gap> Sort(r);
gap> r;
gap> List([1..10],IsPrime);
gap> [1..10];
gap> List([1..10],x->x);

The least obvious thing just now was the command List. Describe what it does.

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;

Discuss!

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
At http://www.math.umn.edu/~webb/GAPfiles/Conway there is a file of GAP code. Create a copy of this file in your GAP file directory, maybe with the name Conway. 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);


The function Conway computes the next term in a sequence devised by John H. Conway. Can you work out what the rule for producing the next term is?