Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->1<!-- %% -->2<!-- %W introduc.tex GAP documentation Thomas Breuer -->3<!-- %W & Frank Celler -->4<!-- %W & Martin Schönert -->5<!-- %W & Heiko Theißen -->6<!-- %% -->7<!-- %H @(#)<M>Id: introduc.tex,v 4.32 2005/05/05 13:22:50 gap Exp </M> -->8<!-- %% -->9<!-- %Y Copyright 1997, Lehrstuhl D für Mathematik, RWTH Aachen, Germany -->10<!-- %% -->11<!-- %% This file contains a tutorial introduction to GAP. -->12<!-- %% -->13<P/>1415<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->16<Chapter Label="A First Session with GAP">17<Heading>A First Session with &GAP;</Heading>18<P/>19This tutorial introduces you to the &GAP; system. It is written with20users in mind who have just managed to start &GAP; for the first time on21their computer and want to learn the basic facts about &GAP; by playing22around with some instructive examples. Therefore, this tutorial contains23at many places examples consisting of several lines of input (which24you should type on your terminal) followed by the corresponding output (25which &GAP; produces as an answer to your input).26<P/>27We encourage you to actually run through these examples on your28computer. This will support your feeling for &GAP; as a tool, which is29the leading aim of this tutorial. Do not believe any statement in it as30long as you cannot verify it for your own version of &GAP;. You will31learn to distinguish between small deviations of the behavior of your32personal &GAP; from the printed examples and serious nonsense.33<P/>34Since the printing routines of &GAP; are in some sense machine dependent35you will for instance encounter a different layout of the printed objects36in different environments. But the contents should always be the same.37In case you encounter serious nonsense it is highly recommended that you38send a bug report to <Email>[email protected]</Email>.39<P/>40The examples in this tutorial should explain everything you have to41know in order to be able to use &GAP;. The reference manual then42gives a more systematic treatment of the various types of objects that43&GAP; can manipulate. It seems desirable neither to start this44systematic course with the most elementary (and most boring)45structures, nor to confront you with all the complex data types before46you know how they are composed from elementary structures. For this47reason this tutorial wants to provide you with a basic understanding48of &GAP; objects, on which the reference manual will then build when49it explains everything in detail. So after having mastered this50tutorial, you can immediately plunge into the exciting parts of &GAP;51and only read detailed information about elementary things (in the52reference manual) when you really need them.53<P/>54Each chapter of this tutorial contains a section with references to the55reference manual at the end.56<P/>57<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->58<Section Label="Starting and Leaving GAP">59<Heading>Starting and Leaving &GAP;</Heading>60<P/>61<Index Key="starting GAP">starting &GAP;</Index>62<Index Key="leaving GAP">leaving &GAP;</Index>63<Index Key="quit"><K>quit</K></Index>64If the program is correctly installed then you usually start &GAP; by65simply66typing <C>gap</C> at the prompt of your operating system followed by the67<B>Return</B> key, sometimes this is also called the <B>Newline</B> key.68<P/>69<Log><![CDATA[70$ gap71]]></Log>72<P/>73&GAP; answers your request with its beautiful banner and then it shows74its own prompt <C>gap></C> asking you for further input.75(You can avoid the banner with the command line option <C>-b</C>;76more command line options are described in77Section <Ref Sect="Command Line Options" BookName="ref"/>.)78<P/>79<Example><![CDATA[80gap>81]]></Example>82<P/>83The usual way to end a &GAP; session is to type <C>quit;</C> at the <C>gap></C>84prompt. Do not omit the semicolon!85<P/>86<Log><![CDATA[87gap> quit;88$89]]></Log>90<P/>91On some systems you could type <B>Ctrl-D</B> to yield the same effect.92In any situation &GAP; is ended by typing <B>Ctrl-C</B> twice within a93second. Here as always, a combination like <B>Ctrl-D</B> means that you have94to press the <B>D</B> key while you hold down the <B>Ctrl</B> key.95<P/>96On some systems (for example the Apple Macintosh) minor changes might be97necessary. This is explained in &GAP; installation instructions (see the98<F>INSTALL</F> file in the &GAP; root directory, or the &GAP; website).99<!-- TODO: put URL -->100<P/>101<Index>whitespace</Index>102In most places <E>whitespace</E> characters (i.e. <B>Space</B>s, <B>Tab</B>s103and <B>Return</B>s) are insignificant for the meaning of &GAP; input.104Identifiers and keywords must however not contain any whitespace.105On the other hand,106sometimes there must be whitespace around identifiers and keywords to107separate them from each other and from numbers. We will use whitespace to108format more complicated commands for better readability.109<P/>110<Index>comments</Index>111A <E>comment</E> in &GAP; starts with the symbol <C>#</C> and continues to the112end of the line. Comments are treated like whitespace by &GAP;. We use113comments in the printed examples in this tutorial to explain certain114lines of input or output.115<!-- % These comments will be printed in a normal font for better readability, -->116<!-- % they start with the symbol #. -->117</Section>118119120<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->121<Section Label="Loading Source Code from a File">122<Heading>Loading Source Code from a File</Heading>123124<Index>loading source code from a file</Index>125<Index>reading source code from a file</Index>126<Index Key="Read"><C>Read</C></Index>127The most convenient way of creating larger pieces of &GAP; code is to128write them to some text file.129For this purpose you can simply use your favorite text editor.130You can load such a file into &GAP; using the131<Ref Func="Read" BookName="ref"/> function:132<P/>133<Log><![CDATA[134gap> Read("../../GAPProgs/Example.g");135]]></Log>136<P/>137You can either give the full absolute path name of the source file or138its relative path name from the &GAP; root directory (the directory139containing <F>bin/</F>, <F>doc/</F>, <F>lib/</F>, etc.).140141</Section>142143144<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->145<Section Label="The Read Evaluate Print Loop">146<Heading>The Read Evaluate Print Loop</Heading>147148<Index>read evaluate print loop</Index>149&GAP; is an interactive system. It continuously executes a150read evaluate print loop. Each expression you type at the keyboard is151read by &GAP;, evaluated, and then the result is shown.152<P/>153The interactive nature of &GAP; allows you to type an expression at the154keyboard and see its value immediately. You can define a function and155apply it to arguments to see how it works. You may even write whole156programs containing lots of functions and test them without leaving the157program.158<P/>159When your program is large it will be more convenient to write it on a160file and then read that file into &GAP;. Preparing your functions in a161file has several advantages. You can compose your functions more162carefully in a file (with your favorite text editor), you can correct163errors without retyping the whole function and you can keep a copy for164later use. Moreover you can write lots of comments into the program text,165which are ignored by &GAP;, but are very useful for human readers of166your program text. &GAP; treats input from a file in the same way that167it treats input from the keyboard. Further details can be found in168section <Ref Func="Read" BookName="ref"/>.169<P/>170A simple calculation with &GAP; is as easy as one can imagine. You type171the problem just after the prompt, terminate it with a semicolon and then172pass the problem to the program with the <B>Return</B> key. For example, to173multiply the difference between 9 and 7 by the sum of 5 and 6, that is to174calculate <M>(9 - 7) * (5 + 6)</M>, you type exactly this last sequence of175symbols followed by <C>;</C> and <B>Return</B>.176<P/>177<Example><![CDATA[178gap> (9 - 7) * (5 + 6);17922180gap>181]]></Example>182<P/>183Then &GAP; echoes the result 22 on the next line and shows with the184prompt that it is ready for the next problem. Henceforth, we will no185longer print this additional prompt.186<P/>187<Index>line editing</Index>188If you make a mistake while typing the line,189but <E>before</E> typing the final <B>Return</B>,190you can use the <B>Delete</B> key (or sometimes <B>Backspace</B> key)191to delete the last typed character.192You can also move the cursor back and forward in the line with <B>Ctrl-B</B>193and <B>Ctrl-F</B> and insert or delete characters anywhere in the line.194The line editing commands are fully described195in section <Ref Sect="Line Editing" BookName="ref"/>.196<P/>197If you did omit the semicolon at the end of the line but have already198typed <B>Return</B>, then &GAP; has read everything you typed, but does199not know that the command is complete. The program is waiting for200further input and indicates this with a partial prompt <C>></C>. This201problem is solved by simply typing the missing semicolon on the next202line of input. Then the result is printed and the normal prompt203returns.204<P/>205<Example><![CDATA[206gap> (9 - 7) * (5 + 6)207> ;20822209]]></Example>210<P/>211So the input can consist of several lines, and &GAP; prints a partial212prompt <C>></C> in each input line except the first, until the command is213completed with a semicolon.214(&GAP; may already evaluate part of the input when <B>Return</B> is typed,215so for long calculations it might take some time until the partial prompt216appears.)217Whenever you see the partial prompt and you cannot decide what &GAP; is218still waiting for, then you have to type semicolons until the normal219prompt returns.220In every situation the exact meaning of the prompt <C>gap></C> is that the221program is waiting for a new problem.222<P/>223But even if you mistyped the command more seriously, you do not have to224type it all again. Suppose you mistyped or forgot the last closing225parenthesis. Then your command is syntactically incorrect and &GAP; will226notice it, incapable of computing the desired result.227<P/>228<Log><![CDATA[229gap> (9 - 7) * (5 + 6;230Syntax error: ) expected231(9 - 7) * (5 + 6;232^233]]></Log>234<P/>235<Index>line editing</Index>236Instead of the result an error message occurs indicating the place where237an unexpected symbol occurred with an arrow sign <C>^</C> under it. As a238computer program cannot know what your intentions really were, this is239only a hint. But in this case &GAP; is right by claiming that there240should be a closing parenthesis before the semicolon. Now you can type241<B>Ctrl-P</B> to recover the last line of input. It will be written after the242prompt with the cursor in the first position. Type <B>Ctrl-E</B> to take the243cursor to the end of the line, then <B>Ctrl-B</B> to move the cursor one244character back. The cursor is now on the position of the semicolon. Enter245the missing parenthesis by simply typing <C>)</C>. Now the line is correct and246may be passed to &GAP; by hitting the <B>Return</B> key. Note that for this247action it is not necessary to move the cursor past the last character of248the input line.249<P/>250Each line of commands you type is sent to &GAP; for evaluation by251pressing <B>Return</B> regardless of the position of the cursor in that line.252We will no longer mention the <B>Return</B> key from now on.253<P/>254<Index>break loops</Index>255Sometimes a syntax error will cause &GAP; to enter a <E>break loop</E>. This256is indicated by the special prompt <C>brk></C>. If another syntax error occurs257while &GAP; is in a break loop, the prompt will change to <C>brk_02></C>,258<C>brk_03></C> and so on. You can leave the current break loop and exit to the259next outer one by either typing <C>quit;</C> or by hitting <B>Ctrl-D</B>.260Eventually &GAP; will return to its normal state and show its normal261prompt <C>gap></C> again.262263</Section>264265266<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->267<Section Label="Constants and Operators">268<Heading>Constants and Operators</Heading>269270<Index>constants</Index><Index>operators</Index>271In an expression like <C>(9 - 7) * (5 + 6)</C> the constants <C>5</C>, <C>6</C>, <C>7</C>,272and <C>9</C> are being composed by the operators <C>+</C>, <C>*</C> and <C>-</C> to result in273a new value.274<P/>275There are three kinds of operators in &GAP;, arithmetical operators,276comparison operators, and logical operators. You have already seen that277it is possible to form the sum, the difference, and the product of two278integer values. There are some more operators applicable to integers in279&GAP;. Of course integers may be divided by each other, possibly280resulting in noninteger rational values.281<P/>282<Example><![CDATA[283gap> 12345/25;2842469/5285]]></Example>286<P/>287Note that the numerator and denominator are divided by their greatest288common divisor and that the result is uniquely represented as a division289instruction.290<P/>291The next self-explanatory example demonstrates negative numbers.292<P/>293<Example><![CDATA[294gap> -3; 17 - 23;295-3296-6297]]></Example>298<P/>299The exponentiation operator is written as <C>^</C>. This operation in300particular might lead to very large numbers. This is no problem for301&GAP; as it can handle numbers of (almost) any size.302<P/>303<Example><![CDATA[304gap> 3^132;305955004950796825236893190701774414011919935138974343129836853841306]]></Example>307<P/>308The <K>mod</K> operator allows you to compute one value modulo another.309<P/>310<Example><![CDATA[311gap> 17 mod 3;3122313]]></Example>314<P/>315Note that there must be whitespace around the keyword <K>mod</K> in this316example since <C>17mod3</C> or <C>17mod</C> would be interpreted as identifiers.317The whitespace around operators that do not consist of letters, e.g.,318the operators <C>*</C> and <C>-</C>, is not necessary.319<P/>320&GAP; knows a precedence between operators that may be overridden by321parentheses.322<P/>323<Example><![CDATA[324gap> (9 - 7) * 5 = 9 - 7 * 5;325false326]]></Example>327<P/>328Besides these arithmetical operators there are comparison operators in &GAP;.329A comparison results in a <E>boolean value</E> which is another kind330of constant.331The comparison operators <C>=</C>, <C><></C>, <C><</C>, <C><=</C>,332<C>></C> and <C>>=</C>, test for equality, inequality, less than,333less than or equal, greater than and greater than or equal, respectively.334<P/>335<Example><![CDATA[336gap> 10^5 < 10^4;337false338]]></Example>339<P/>340The boolean values <K>true</K> and <K>false</K> can be manipulated via341logical operators, i. e., the unary operator <K>not</K> and the342binary operators <K>and</K> and <K>or</K>.343Of course boolean values can be compared, too.344<P/>345<Example><![CDATA[346gap> not true; true and false; true or false;347false348false349true350gap> 10 > 0 and 10 < 100;351true352]]></Example>353<P/>354Another important type of constants in &GAP; are <E>permutations</E>. They355are written in cycle notation and they can be multiplied.356<P/>357<Example><![CDATA[358gap> (1,2,3);359(1,2,3)360gap> (1,2,3) * (1,2);361(2,3)362]]></Example>363<P/>364The inverse of the permutation <C>(1,2,3)</C> is denoted by <C>(1,2,3)^-1</C>.365Moreover the caret operator <C>^</C> is used to determine the image of a point366under a permutation and to conjugate one permutation by another.367<P/>368<Example><![CDATA[369gap> (1,2,3)^-1;370(1,3,2)371gap> 2^(1,2,3);3723373gap> (1,2,3)^(1,2);374(1,3,2)375]]></Example>376<P/>377The various other constants that &GAP; can deal with will be introduced378when they are used, for example there are elements of finite fields379such as <C>Z(8)</C>, and complex roots of unity such as <C>E(4)</C>.380<P/>381The last type of constants we want to mention here are the382<E>characters</E>, which are simply objects in &GAP; that represent arbitrary383characters from the character set of the operating system. Character384literals can be entered in &GAP; by enclosing the character in385<E>singlequotes</E> <C>'</C>.386<P/>387<Example><![CDATA[388gap> 'a';389'a'390gap> '*';391'*'392]]></Example>393<P/>394There are no operators defined for characters except that characters can395be compared.396<P/>397<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->398In this section you have seen that values may be preceded by unary399operators and combined by binary operators placed between the operands.400There are rules for precedence which may be overridden by parentheses. A401comparison results in a boolean value. Boolean values are combined via402logical operators. Moreover you have seen that &GAP; handles numbers of403arbitrary size. Numbers and boolean values are constants. There are404other types of constants in &GAP; like permutations. You are now in a405position to use &GAP; as a simple desktop calculator.406407</Section>408409410<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->411<Section Label="Variables versus Objects">412<Heading>Variables versus Objects</Heading>413414<Index>variables</Index><Index>assignment</Index><Index>identifier</Index><Index>objects</Index>415<Index Subkey="vs. variables">objects</Index>416The constants described in the last section are specified by certain417combinations of digits and minus signs (in the case of integers) or418digits, commas and parentheses (in the case of permutations). These419sequences of characters always have the same meaning to &GAP;. On the420other hand, there are <E>variables</E>, specified by a sequence of letters and421digits (including at least one letter), and their meaning depends on what422has been assigned to them. An <E>assignment</E> is done by a &GAP; command423<C><A>sequence_of_letters_and_digits</A> := <A>meaning</A></C>, where the sequence on424the left hand side is called the <E>identifier</E> of the variable and it serves425as its name. The meaning on the right hand side can be a constant like an426integer or a permutation, but it can also be almost any other &GAP;427object. From now on, we will use the term <E>object</E> to denote something428that can be assigned to a variable.429<P/>430There must be no whitespace between the <C>:</C> and the <C>=</C> in the assignment431operator. Also do not confuse the assignment operator with the single432equality sign <C>=</C> which in &GAP; is only used for the test of equality.433<P/>434<Example><![CDATA[435gap> a:= (9 - 7) * (5 + 6);43622437gap> a;43822439gap> a * (a + 1);440506441gap> a = 10;442false443gap> a:= 10;44410445gap> a * (a + 1);446110447]]></Example>448<P/>449After an assignment the assigned object is echoed on the next line. The450printing of the object of a statement may be in every case prevented by451typing a double semicolon.452<P/>453<Example><![CDATA[454gap> w:= 2;;455]]></Example>456<P/>457After the assignment the variable evaluates to that object if evaluated.458Thus it is possible to refer to that object by the name of the variable459in any situation.460<P/>461This is in fact the whole secret of an assignment. An identifier is bound462to an object and from this moment points to that object. Nothing more.463This binding is changed by the next assignment to that identifier. An464identifier does not denote a block of memory as in some other programming465languages. It simply points to an object, which has been given its place466in memory by the &GAP; storage manager. This place may change during a467&GAP; session, but that doesn't bother the identifier. <E>The identifier468points to the object, not to a place in the memory.</E>469<P/>470For the same reason it is not the identifier that has a type but the471object. This means on the other hand that the identifier <C>a</C> which now472is bound to an integer object may in the same session point to any other473object regardless of its type.474<P/>475Identifiers may be sequences of letters and digits containing at least476one letter. For example <C>abc</C> and <C>a0bc1</C> are valid identifiers. But477also <C>123a</C> is a valid identifier as it cannot be confused with any478number. Just <C>1234</C> indicates the number 1234 and cannot be at the same479time the name of a variable.480<P/>481Since &GAP; distinguishes upper and lower case, <C>a1</C> and <C>A1</C> are482different identifiers. Keywords such as <K>quit</K> must not be used as483identifiers. You will see more keywords in the following sections.484<P/>485In the remaining part of this manual we will ignore the difference486between variables, their names (identifiers), and the objects they point487to. It may be useful to think from time to time about what is really488meant by terms such as <Q>the integer <C>w</C></Q>.489<P/>490There are some predefined variables coming with &GAP;. Many of them you491will find in the remaining chapters of this manual, since functions are492also referred to via identifiers.493<P/>494You can get an overview of <E>all</E> &GAP; variables by entering495<C>NamesGVars()</C>. Many of these are predefined. If you are interested in496the variables you have defined yourself in the current &GAP; session,497you can enter <C>NamesUserGVars()</C>.498<P/>499<Log><![CDATA[500gap> NamesUserGVars();501[ "a", "w" ]502]]></Log>503<P/>504This seems to be the right place to state the following rule:505The name of every global variable in the &GAP; library starts with a506<E>capital letter</E>.507Thus if you choose only names starting with a small letter for your own508variables you will not attempt to overwrite any predefined variable.509(Note that most of the predefined variables are read-only,510and trying to change their values will result in an error message.)511<P/>512There are some further interesting variables one of which will be513introduced now.514<P/>515<Index Key="last"><C>last</C></Index>516<Index Key="last2"><C>last2</C></Index>517<Index Key="last3"><C>last3</C></Index>518Whenever &GAP; returns an object by printing it on the next line this519object is assigned to the variable <C>last</C>. So if you computed520<P/>521<Example><![CDATA[522gap> (9 - 7) * (5 + 6);52322524]]></Example>525<P/>526and forgot to assign the object to the variable <C>a</C> for further use, you527can still do it by the following assignment.528<P/>529<Example><![CDATA[530gap> a:= last;53122532]]></Example>533<P/>534Moreover there are variables <C>last2</C> and <C>last3</C>, you can guess their values.535<P/>536<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->537In this section you have seen how to assign objects to variables. These538objects can later be accessed through the name of the variable, its539identifier. You have also encountered the useful concept of the <C>last</C>540variables storing the latest returned objects. And you have learned that541a double semicolon prevents the result of a statement from being printed.542543</Section>544545546<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->547<Section Label="Objects vs. Elements">548<Heading>Objects vs. Elements</Heading>549550<Index Subkey="vs. elements">objects</Index>551<Index>elements</Index>552In the last section we mentioned that every object is given a certain553place in memory by the &GAP; storage manager (although that place may554change in the course of a &GAP; session). In this sense, objects at555different places in memory are never equal, and if the object pointed to556by the variable <C>a</C> (to be more precise, the variable with identifier557<C>a</C>) is equal to the object pointed to by the variable <C>b</C>, then we558should better say that they are not only equal but <E>identical</E>. &GAP;559provides the function <Ref Func="IsIdenticalObj" BookName="ref"/>560to test whether this is the case.561<P/>562<Example><![CDATA[563gap> a:= (1,2);; IsIdenticalObj( a, a );564true565gap> b:= (1,2);; IsIdenticalObj( a, b );566false567gap> b:= a;; IsIdenticalObj( a, b );568true569]]></Example>570<P/>571<Index Key="IsIdenticalObj"><C>IsIdenticalObj</C></Index>572<P/>573As the above example indicates, &GAP;574objects <A>a</A> and <A>b</A> can be unequal although they are equal from a575mathematical point of view, i.e., although we should have <A>a</A> = <A>b</A>. It576may be that the objects <A>a</A> and <A>b</A> are stored in different places in577memory, or it may be that we have an equivalence relation defined on the578set of objects under which <A>a</A> and <A>b</A> belong to the same equivalence579class. For example, if <M><A>a</A> = x^3</M> and <M><A>b</A> = x^{{-5}}</M> are words in580the finitely presented group <M>\langle x \mid x^2 = 1 \rangle</M>,581we would have <A>a</A> = <A>b</A> in that group.582<P/>583&GAP; uses the equality operator <C>=</C> to denote such a mathematical584equality, <E>not</E> the identity of objects. Hence we often have <C><A>a</A> = <A>b</A></C>585although <C>IsIdenticalObj( <A>a</A>, <A>b</A> ) = false</C>. The operator <C>=</C> defines586an equivalence relation on the set of all &GAP; objects, and we call the587corresponding equivalence classes <E>elements</E>. Phrasing it differently,588the same element may be represented by various &GAP; objects.589<P/>590Non-trivial examples of elements that are represented by different591objects (objects that really look different, not ones that are merely592stored in different memory places) will occur only when we will be593considering composite objects such as lists or domains.594595</Section>596597598<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->599<Section Label="About Functions">600<Heading>About Functions</Heading>601602A program written in the &GAP; language is called a <E>function</E>.603Functions are special &GAP; objects. Most of them behave like604mathematical functions. They are applied to objects and will return a605new object depending on the input.606The function <Ref Func="Factorial" BookName="ref"/>, for607example, can be applied to an integer and will return the factorial of608this integer.609<P/>610<Example><![CDATA[611gap> Factorial(17);612355687428096000613]]></Example>614<P/>615Applying a function to arguments means to write the arguments in616parentheses following the function. Several arguments are separated by617commas, as for the function618<Ref Func="Gcd" BookName="ref"/> which computes the greatest common619divisor of two integers.620<P/>621<Example><![CDATA[622gap> Gcd(1234, 5678);6232624]]></Example>625<P/>626There are other functions that do not return an object but only627produce a side effect, for example changing one of their arguments.628These functions are sometimes called procedures.629The function <Ref Func="Print" BookName="ref"/>630is only called for the side effect of printing something on the screen.631<P/>632<Example><![CDATA[633gap> Print(1234, "\n");6341234635]]></Example>636<P/>637In order to be able to compose arbitrary text with638<Ref Func="Print" BookName="ref"/>, this function639itself will not produce a line break after printing. Thus we had another640newline character <C>"\n"</C> printed to start a new line.641<P/>642Some functions will both change an argument and return an object such as643the function <Ref Func="Sortex" BookName="ref"/> that sorts a list644and returns the permutation of645the list elements that it has performed. You will not understand right646now what it means to change an object. We will return to this subject647several times in the next sections.648<P/>649<Index>maps-to operator</Index>650A comfortable way to define a function yourself is the <E>maps-to</E> operator651<C>-></C> consisting of a minus sign and a greater sign with no whitespace652between them. The function <C>cubed</C> which maps a number to its cube is653defined on the following line.654<P/>655<Example><![CDATA[656gap> cubed:= x -> x^3;657function( x ) ... end658]]></Example>659<P/>660After the function has been defined, it can now be applied.661<P/>662<Example><![CDATA[663gap> cubed(5);664125665]]></Example>666<P/>667More complicated functions, especially functions with more than one668argument cannot be defined in this way.669You will see how to write your own &GAP; functions in670Section <Ref Sect="Writing Functions"/>.671<P/>672<!-- %% Summary %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->673In this section you have seen &GAP; objects of type function. You have674learned how to apply a function to arguments. This yields as result a675new object or a side effect. A side effect may change an argument of the676function. Moreover you have seen an easy way to define a function in677&GAP; with the maps-to operator.678679</Section>680681682<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->683<Section Label="Help">684<Heading>Help</Heading>685686The content of the &GAP; manuals is also available as on-line help. A687&GAP; session loads a long list of index entries. This typically contains688all chapter and section headers, all names of documented functions,689operations and so on, as well as some explicit index entries defined in the690manuals.691<P/>692The format of a query is as follows.693<P/>694<C>?[<A>book</A>:][?]<A>topic</A></C>695<P/>696A simple example would be to type <C>?help</C> at the &GAP; prompt. If there is697a single section with index entry <A>topic</A> then this is displayed directly.698<P/>699If there are several matches you get an overview like in the example below.700<P/>701<Log><![CDATA[702gap> ?sets703Help: several entries match this topic - type ?2 to get match [2]704705[1] Tutorial: Sets706[2] Reference: Sets707[3] Reference: sets708[4] Reference: Sets of Subgroups709[5] Reference: setstabilizer710]]></Log>711<P/>712&GAP;'s manuals consist of several <E>books</E>, which are indicated before the713colon in the list above. A help query can be restricted to one book by using714the optional <A>book</A>: part. For example <C>?tut : sets</C> will display the first715of these help sections. More precisely, the parts of the string <A>book</A> which716are separated by white space are interpreted as beginnings of the first717words in the name of the book. Try <C>?books</C> to see the list of available718books and their names.719<P/>720The search for a matching <A>topic</A> (and optional <A>book</A>) is done <E>case721insensitively</E>. If there is another <C>?</C> before the <A>topic</A>, then a722<E>substring search</E> for <A>topic</A> is performed on all index entries. Otherwise723the parts of <A>topic</A> which are separated by white space are considered as724<E>beginnings of the first words</E> in an index entry.725<P/>726White space is normalized in the search string (and the index entries).727<P/>728<E>Examples.</E> All the following queries lead to the chapter of the reference729manual which explains the use of &GAP;'s help system in more detail.730<P/>731<Log><![CDATA[732gap> ?Reference: The Help System733gap> ? REF : t h s734gap> ?ref:? help system735]]></Log>736<P/>737The query <C>??sets</C> shows all help sections in all books whose index entries738contain the substring <C>sets</C>.739<P/>740As mentioned in the example above a complete list of commands for the help741system is available in Section <C>?Ref: The Help System</C> of the reference742manual. In particular there are commands to browse through the help743sections, see <C>?Ref: Browsing through the Sections</C> and there is a way to744influence the way <E>how</E> the help sections are displayed, see <C>?Ref:745SetHelpViewer</C>. For example you can use an external pager program, a Web746browser, <C>dvi</C>-previewer and/or <C>pdf</C>-viewer for reading &GAP;'s online747help.748749</Section>750751752<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->753<Section Label="Further Information introducing the System">754<Heading>Further Information introducing the System</Heading>755756For large amounts of input data, it might be advisable to write your757input first into a file, and then read this into &GAP;;758see <Ref Func="Read" BookName="ref"/>, <Ref Func="Edit" BookName="ref"/> for this.759<P/>760The definition of the &GAP; syntax can be looked up in761Chapter <Ref Chap="The Programming Language" BookName="ref"/>.762A complete list of command line editing facilities is found763in Section <Ref Sect="Line Editing" BookName="ref"/>.764The break loop is described in Section <Ref Sect="Break Loops" BookName="ref"/>.765<P/>766Operators are explained in more detail in Sections <Ref Sect="Expressions" BookName="ref"/> and767<Ref Sect="Comparisons" BookName="ref"/>.768You will find more information about boolean values in769Chapters <Ref Chap="Booleans" BookName="ref"/> and <Ref Chap="Boolean Lists" BookName="ref"/>.770Permutations are described in Chapter <Ref Chap="Permutations" BookName="ref"/> and characters771in Chapter <Ref Chap="Strings and Characters" BookName="ref"/>.772<P/>773Variables and assignments are described in more detail in <Ref Sect="Variables" BookName="ref"/>774and <Ref Sect="Assignments" BookName="ref"/>.775A complete list of keywords is contained in <Ref Sect="Keywords" BookName="ref"/>.776<P/>777More about functions can be found in <Ref Sect="Function Calls" BookName="ref"/>778and <Ref Sect="Procedure Calls" BookName="ref"/>.779780</Section>781</Chapter>782783784<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->785<!-- %% -->786<!-- %E -->787788789790