/usr/share/gap/doc/tut/chap2.html is in gap-doc 4r7p9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>GAP (tut) - Chapter 2: A First Session with GAP</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="generator" content="GAPDoc2HTML" />
<link rel="stylesheet" type="text/css" href="manual.css" />
<script src="manual.js" type="text/javascript"></script>
<script type="text/javascript">overwriteStyle();</script>
</head>
<body class="chap2" onload="jscontent()">
<div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
<div class="chlinkprevnexttop"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap1.html">[Previous Chapter]</a> <a href="chap3.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap2_mj.html">[MathJax on]</a></p>
<p><a id="X7E1648BF8446FBC2" name="X7E1648BF8446FBC2"></a></p>
<div class="ChapSects"><a href="chap2.html#X7E1648BF8446FBC2">2 <span class="Heading">A First Session with <strong class="pkg">GAP</strong></span></a>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X7B5258097F48EFB6">2.1 <span class="Heading">Starting and Leaving <strong class="pkg">GAP</strong></span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X7B8D5EC27E33AF6D">2.2 <span class="Heading">Loading Source Code from a File</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X7C7388A683D15599">2.3 <span class="Heading">The Read Evaluate Print Loop</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X86CA8242832F5F71">2.4 <span class="Heading">Constants and Operators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X82F47EA88347AC2D">2.5 <span class="Heading">Variables versus Objects</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X7F36AE347AD3CDEC">2.6 <span class="Heading">Objects vs. Elements</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X796E055D86856B42">2.7 <span class="Heading">About Functions</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X7A7EADB77FF38BC9">2.8 <span class="Heading">Help</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap2.html#X798515EF7D762548">2.9 <span class="Heading">Further Information introducing the System</span></a>
</span>
</div>
</div>
<h3>2 <span class="Heading">A First Session with <strong class="pkg">GAP</strong></span></h3>
<p>This tutorial introduces you to the <strong class="pkg">GAP</strong> system. It is written with users in mind who have just managed to start <strong class="pkg">GAP</strong> for the first time on their computer and want to learn the basic facts about <strong class="pkg">GAP</strong> by playing around with some instructive examples. Therefore, this tutorial contains at many places examples consisting of several lines of input (which you should type on your terminal) followed by the corresponding output ( which <strong class="pkg">GAP</strong> produces as an answer to your input).</p>
<p>We encourage you to actually run through these examples on your computer. This will support your feeling for <strong class="pkg">GAP</strong> as a tool, which is the leading aim of this tutorial. Do not believe any statement in it as long as you cannot verify it for your own version of <strong class="pkg">GAP</strong>. You will learn to distinguish between small deviations of the behavior of your personal <strong class="pkg">GAP</strong> from the printed examples and serious nonsense.</p>
<p>Since the printing routines of <strong class="pkg">GAP</strong> are in some sense machine dependent you will for instance encounter a different layout of the printed objects in different environments. But the contents should always be the same. In case you encounter serious nonsense it is highly recommended that you send a bug report to <span class="URL"><a href="mailto:support@gap-system.org">support@gap-system.org</a></span>.</p>
<p>The examples in this tutorial should explain everything you have to know in order to be able to use <strong class="pkg">GAP</strong>. The reference manual then gives a more systematic treatment of the various types of objects that <strong class="pkg">GAP</strong> can manipulate. It seems desirable neither to start this systematic course with the most elementary (and most boring) structures, nor to confront you with all the complex data types before you know how they are composed from elementary structures. For this reason this tutorial wants to provide you with a basic understanding of <strong class="pkg">GAP</strong> objects, on which the reference manual will then build when it explains everything in detail. So after having mastered this tutorial, you can immediately plunge into the exciting parts of <strong class="pkg">GAP</strong> and only read detailed information about elementary things (in the reference manual) when you really need them.</p>
<p>Each chapter of this tutorial contains a section with references to the reference manual at the end.</p>
<p><a id="X7B5258097F48EFB6" name="X7B5258097F48EFB6"></a></p>
<h4>2.1 <span class="Heading">Starting and Leaving <strong class="pkg">GAP</strong></span></h4>
<p>If the program is correctly installed then you usually start <strong class="pkg">GAP</strong> by simply typing <code class="code">gap</code> at the prompt of your operating system followed by the <strong class="button">Return</strong> key, sometimes this is also called the <strong class="button">Newline</strong> key.</p>
<div class="example"><pre>
$ gap
</pre></div>
<p><strong class="pkg">GAP</strong> answers your request with its beautiful banner and then it shows its own prompt <code class="code">gap></code> asking you for further input. (You can avoid the banner with the command line option <code class="code">-b</code>; more command line options are described in Section <a href="../../doc/ref/chap3.html#X782751D5858A6EAF"><span class="RefLink">Reference: Command Line Options</span></a>.)</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
</pre></div>
<p>The usual way to end a <strong class="pkg">GAP</strong> session is to type <code class="code">quit;</code> at the <code class="code">gap></code> prompt. Do not omit the semicolon!</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">quit;</span>
$
</pre></div>
<p>On some systems you could type <strong class="button">Ctrl-D</strong> to yield the same effect. In any situation <strong class="pkg">GAP</strong> is ended by typing <strong class="button">Ctrl-C</strong> twice within a second. Here as always, a combination like <strong class="button">Ctrl-D</strong> means that you have to press the <strong class="button">D</strong> key while you hold down the <strong class="button">Ctrl</strong> key.</p>
<p>On some systems (for example the Apple Macintosh) minor changes might be necessary. This is explained in <strong class="pkg">GAP</strong> installation instructions (see the <code class="file">INSTALL</code> file in the <strong class="pkg">GAP</strong> root directory, or the <strong class="pkg">GAP</strong> website).</p>
<p>In most places <em>whitespace</em> characters (i.e. <strong class="button">Space</strong>s, <strong class="button">Tab</strong>s and <strong class="button">Return</strong>s) are insignificant for the meaning of <strong class="pkg">GAP</strong> input. Identifiers and keywords must however not contain any whitespace. On the other hand, sometimes there must be whitespace around identifiers and keywords to separate them from each other and from numbers. We will use whitespace to format more complicated commands for better readability.</p>
<p>A <em>comment</em> in <strong class="pkg">GAP</strong> starts with the symbol <code class="code">#</code> and continues to the end of the line. Comments are treated like whitespace by <strong class="pkg">GAP</strong>. We use comments in the printed examples in this tutorial to explain certain lines of input or output.</p>
<p><a id="X7B8D5EC27E33AF6D" name="X7B8D5EC27E33AF6D"></a></p>
<h4>2.2 <span class="Heading">Loading Source Code from a File</span></h4>
<p>The most convenient way of creating larger pieces of <strong class="pkg">GAP</strong> code is to write them to some text file. For this purpose you can simply use your favorite text editor. You can load such a file into <strong class="pkg">GAP</strong> using the <code class="func">Read</code> (<a href="../../doc/ref/chap9.html#X8373AC6B7D5F9167"><span class="RefLink">Reference: Read</span></a>) function:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Read("../../GAPProgs/Example.g");</span>
</pre></div>
<p>You can either give the full absolute path name of the source file or its relative path name from the <strong class="pkg">GAP</strong> root directory (the directory containing <code class="file">bin/</code>, <code class="file">doc/</code>, <code class="file">lib/</code>, etc.).</p>
<p><a id="X7C7388A683D15599" name="X7C7388A683D15599"></a></p>
<h4>2.3 <span class="Heading">The Read Evaluate Print Loop</span></h4>
<p><strong class="pkg">GAP</strong> is an interactive system. It continuously executes a read evaluate print loop. Each expression you type at the keyboard is read by <strong class="pkg">GAP</strong>, evaluated, and then the result is shown.</p>
<p>The interactive nature of <strong class="pkg">GAP</strong> allows you to type an expression at the keyboard and see its value immediately. You can define a function and apply it to arguments to see how it works. You may even write whole programs containing lots of functions and test them without leaving the program.</p>
<p>When your program is large it will be more convenient to write it on a file and then read that file into <strong class="pkg">GAP</strong>. Preparing your functions in a file has several advantages. You can compose your functions more carefully in a file (with your favorite text editor), you can correct errors without retyping the whole function and you can keep a copy for later use. Moreover you can write lots of comments into the program text, which are ignored by <strong class="pkg">GAP</strong>, but are very useful for human readers of your program text. <strong class="pkg">GAP</strong> treats input from a file in the same way that it treats input from the keyboard. Further details can be found in section <code class="func">Read</code> (<a href="../../doc/ref/chap9.html#X8373AC6B7D5F9167"><span class="RefLink">Reference: Read</span></a>).</p>
<p>A simple calculation with <strong class="pkg">GAP</strong> is as easy as one can imagine. You type the problem just after the prompt, terminate it with a semicolon and then pass the problem to the program with the <strong class="button">Return</strong> key. For example, to multiply the difference between 9 and 7 by the sum of 5 and 6, that is to calculate <span class="SimpleMath">(9 - 7) * (5 + 6)</span>, you type exactly this last sequence of symbols followed by <code class="code">;</code> and <strong class="button">Return</strong>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(9 - 7) * (5 + 6);</span>
22
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
</pre></div>
<p>Then <strong class="pkg">GAP</strong> echoes the result 22 on the next line and shows with the prompt that it is ready for the next problem. Henceforth, we will no longer print this additional prompt.</p>
<p>If you make a mistake while typing the line, but <em>before</em> typing the final <strong class="button">Return</strong>, you can use the <strong class="button">Delete</strong> key (or sometimes <strong class="button">Backspace</strong> key) to delete the last typed character. You can also move the cursor back and forward in the line with <strong class="button">Ctrl-B</strong> and <strong class="button">Ctrl-F</strong> and insert or delete characters anywhere in the line. The line editing commands are fully described in section <a href="../../doc/ref/chap6.html#X82234FD181899530"><span class="RefLink">Reference: Line Editing</span></a>.</p>
<p>If you did omit the semicolon at the end of the line but have already typed <strong class="button">Return</strong>, then <strong class="pkg">GAP</strong> has read everything you typed, but does not know that the command is complete. The program is waiting for further input and indicates this with a partial prompt <code class="code">></code>. This problem is solved by simply typing the missing semicolon on the next line of input. Then the result is printed and the normal prompt returns.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(9 - 7) * (5 + 6)</span>
<span class="GAPprompt">></span> <span class="GAPinput">;</span>
22
</pre></div>
<p>So the input can consist of several lines, and <strong class="pkg">GAP</strong> prints a partial prompt <code class="code">></code> in each input line except the first, until the command is completed with a semicolon. (<strong class="pkg">GAP</strong> may already evaluate part of the input when <strong class="button">Return</strong> is typed, so for long calculations it might take some time until the partial prompt appears.) Whenever you see the partial prompt and you cannot decide what <strong class="pkg">GAP</strong> is still waiting for, then you have to type semicolons until the normal prompt returns. In every situation the exact meaning of the prompt <code class="code">gap></code> is that the program is waiting for a new problem.</p>
<p>But even if you mistyped the command more seriously, you do not have to type it all again. Suppose you mistyped or forgot the last closing parenthesis. Then your command is syntactically incorrect and <strong class="pkg">GAP</strong> will notice it, incapable of computing the desired result.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(9 - 7) * (5 + 6;</span>
Syntax error: ) expected
(9 - 7) * (5 + 6;
^
</pre></div>
<p>Instead of the result an error message occurs indicating the place where an unexpected symbol occurred with an arrow sign <code class="code">^</code> under it. As a computer program cannot know what your intentions really were, this is only a hint. But in this case <strong class="pkg">GAP</strong> is right by claiming that there should be a closing parenthesis before the semicolon. Now you can type <strong class="button">Ctrl-P</strong> to recover the last line of input. It will be written after the prompt with the cursor in the first position. Type <strong class="button">Ctrl-E</strong> to take the cursor to the end of the line, then <strong class="button">Ctrl-B</strong> to move the cursor one character back. The cursor is now on the position of the semicolon. Enter the missing parenthesis by simply typing <code class="code">)</code>. Now the line is correct and may be passed to <strong class="pkg">GAP</strong> by hitting the <strong class="button">Return</strong> key. Note that for this action it is not necessary to move the cursor past the last character of the input line.</p>
<p>Each line of commands you type is sent to <strong class="pkg">GAP</strong> for evaluation by pressing <strong class="button">Return</strong> regardless of the position of the cursor in that line. We will no longer mention the <strong class="button">Return</strong> key from now on.</p>
<p>Sometimes a syntax error will cause <strong class="pkg">GAP</strong> to enter a <em>break loop</em>. This is indicated by the special prompt <code class="code">brk></code>. If another syntax error occurs while <strong class="pkg">GAP</strong> is in a break loop, the prompt will change to <code class="code">brk_02></code>, <code class="code">brk_03></code> and so on. You can leave the current break loop and exit to the next outer one by either typing <code class="code">quit;</code> or by hitting <strong class="button">Ctrl-D</strong>. Eventually <strong class="pkg">GAP</strong> will return to its normal state and show its normal prompt <code class="code">gap></code> again.</p>
<p><a id="X86CA8242832F5F71" name="X86CA8242832F5F71"></a></p>
<h4>2.4 <span class="Heading">Constants and Operators</span></h4>
<p>In an expression like <code class="code">(9 - 7) * (5 + 6)</code> the constants <code class="code">5</code>, <code class="code">6</code>, <code class="code">7</code>, and <code class="code">9</code> are being composed by the operators <code class="code">+</code>, <code class="code">*</code> and <code class="code">-</code> to result in a new value.</p>
<p>There are three kinds of operators in <strong class="pkg">GAP</strong>, arithmetical operators, comparison operators, and logical operators. You have already seen that it is possible to form the sum, the difference, and the product of two integer values. There are some more operators applicable to integers in <strong class="pkg">GAP</strong>. Of course integers may be divided by each other, possibly resulting in noninteger rational values.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">12345/25;</span>
2469/5
</pre></div>
<p>Note that the numerator and denominator are divided by their greatest common divisor and that the result is uniquely represented as a division instruction.</p>
<p>The next self-explanatory example demonstrates negative numbers.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">-3; 17 - 23;</span>
-3
-6
</pre></div>
<p>The exponentiation operator is written as <code class="code">^</code>. This operation in particular might lead to very large numbers. This is no problem for <strong class="pkg">GAP</strong> as it can handle numbers of (almost) any size.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">3^132;</span>
955004950796825236893190701774414011919935138974343129836853841
</pre></div>
<p>The <code class="keyw">mod</code> operator allows you to compute one value modulo another.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">17 mod 3;</span>
2
</pre></div>
<p>Note that there must be whitespace around the keyword <code class="keyw">mod</code> in this example since <code class="code">17mod3</code> or <code class="code">17mod</code> would be interpreted as identifiers. The whitespace around operators that do not consist of letters, e.g., the operators <code class="code">*</code> and <code class="code">-</code>, is not necessary.</p>
<p><strong class="pkg">GAP</strong> knows a precedence between operators that may be overridden by parentheses.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(9 - 7) * 5 = 9 - 7 * 5;</span>
false
</pre></div>
<p>Besides these arithmetical operators there are comparison operators in <strong class="pkg">GAP</strong>. A comparison results in a <em>boolean value</em> which is another kind of constant. The comparison operators <code class="code">=</code>, <code class="code"><></code>, <code class="code"><</code>, <code class="code"><=</code>, <code class="code">></code> and <code class="code">>=</code>, test for equality, inequality, less than, less than or equal, greater than and greater than or equal, respectively.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">10^5 < 10^4;</span>
false
</pre></div>
<p>The boolean values <code class="keyw">true</code> and <code class="keyw">false</code> can be manipulated via logical operators, i. e., the unary operator <code class="keyw">not</code> and the binary operators <code class="keyw">and</code> and <code class="keyw">or</code>. Of course boolean values can be compared, too.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">not true; true and false; true or false;</span>
false
false
true
<span class="GAPprompt">gap></span> <span class="GAPinput">10 > 0 and 10 < 100;</span>
true
</pre></div>
<p>Another important type of constants in <strong class="pkg">GAP</strong> are <em>permutations</em>. They are written in cycle notation and they can be multiplied.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(1,2,3);</span>
(1,2,3)
<span class="GAPprompt">gap></span> <span class="GAPinput">(1,2,3) * (1,2);</span>
(2,3)
</pre></div>
<p>The inverse of the permutation <code class="code">(1,2,3)</code> is denoted by <code class="code">(1,2,3)^-1</code>. Moreover the caret operator <code class="code">^</code> is used to determine the image of a point under a permutation and to conjugate one permutation by another.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(1,2,3)^-1;</span>
(1,3,2)
<span class="GAPprompt">gap></span> <span class="GAPinput">2^(1,2,3);</span>
3
<span class="GAPprompt">gap></span> <span class="GAPinput">(1,2,3)^(1,2);</span>
(1,3,2)
</pre></div>
<p>The various other constants that <strong class="pkg">GAP</strong> can deal with will be introduced when they are used, for example there are elements of finite fields such as <code class="code">Z(8)</code>, and complex roots of unity such as <code class="code">E(4)</code>.</p>
<p>The last type of constants we want to mention here are the <em>characters</em>, which are simply objects in <strong class="pkg">GAP</strong> that represent arbitrary characters from the character set of the operating system. Character literals can be entered in <strong class="pkg">GAP</strong> by enclosing the character in <em>singlequotes</em> <code class="code">'</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">'a';</span>
'a'
<span class="GAPprompt">gap></span> <span class="GAPinput">'*';</span>
'*'
</pre></div>
<p>There are no operators defined for characters except that characters can be compared.</p>
<p>In this section you have seen that values may be preceded by unary operators and combined by binary operators placed between the operands. There are rules for precedence which may be overridden by parentheses. A comparison results in a boolean value. Boolean values are combined via logical operators. Moreover you have seen that <strong class="pkg">GAP</strong> handles numbers of arbitrary size. Numbers and boolean values are constants. There are other types of constants in <strong class="pkg">GAP</strong> like permutations. You are now in a position to use <strong class="pkg">GAP</strong> as a simple desktop calculator.</p>
<p><a id="X82F47EA88347AC2D" name="X82F47EA88347AC2D"></a></p>
<h4>2.5 <span class="Heading">Variables versus Objects</span></h4>
<p>The constants described in the last section are specified by certain combinations of digits and minus signs (in the case of integers) or digits, commas and parentheses (in the case of permutations). These sequences of characters always have the same meaning to <strong class="pkg">GAP</strong>. On the other hand, there are <em>variables</em>, specified by a sequence of letters and digits (including at least one letter), and their meaning depends on what has been assigned to them. An <em>assignment</em> is done by a <strong class="pkg">GAP</strong> command <code class="code"><var class="Arg">sequence_of_letters_and_digits</var> := <var class="Arg">meaning</var></code>, where the sequence on the left hand side is called the <em>identifier</em> of the variable and it serves as its name. The meaning on the right hand side can be a constant like an integer or a permutation, but it can also be almost any other <strong class="pkg">GAP</strong> object. From now on, we will use the term <em>object</em> to denote something that can be assigned to a variable.</p>
<p>There must be no whitespace between the <code class="code">:</code> and the <code class="code">=</code> in the assignment operator. Also do not confuse the assignment operator with the single equality sign <code class="code">=</code> which in <strong class="pkg">GAP</strong> is only used for the test of equality.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a:= (9 - 7) * (5 + 6);</span>
22
<span class="GAPprompt">gap></span> <span class="GAPinput">a;</span>
22
<span class="GAPprompt">gap></span> <span class="GAPinput">a * (a + 1);</span>
506
<span class="GAPprompt">gap></span> <span class="GAPinput">a = 10;</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">a:= 10;</span>
10
<span class="GAPprompt">gap></span> <span class="GAPinput">a * (a + 1);</span>
110
</pre></div>
<p>After an assignment the assigned object is echoed on the next line. The printing of the object of a statement may be in every case prevented by typing a double semicolon.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">w:= 2;; </span>
</pre></div>
<p>After the assignment the variable evaluates to that object if evaluated. Thus it is possible to refer to that object by the name of the variable in any situation.</p>
<p>This is in fact the whole secret of an assignment. An identifier is bound to an object and from this moment points to that object. Nothing more. This binding is changed by the next assignment to that identifier. An identifier does not denote a block of memory as in some other programming languages. It simply points to an object, which has been given its place in memory by the <strong class="pkg">GAP</strong> storage manager. This place may change during a <strong class="pkg">GAP</strong> session, but that doesn't bother the identifier. <em>The identifier points to the object, not to a place in the memory.</em></p>
<p>For the same reason it is not the identifier that has a type but the object. This means on the other hand that the identifier <code class="code">a</code> which now is bound to an integer object may in the same session point to any other object regardless of its type.</p>
<p>Identifiers may be sequences of letters and digits containing at least one letter. For example <code class="code">abc</code> and <code class="code">a0bc1</code> are valid identifiers. But also <code class="code">123a</code> is a valid identifier as it cannot be confused with any number. Just <code class="code">1234</code> indicates the number 1234 and cannot be at the same time the name of a variable.</p>
<p>Since <strong class="pkg">GAP</strong> distinguishes upper and lower case, <code class="code">a1</code> and <code class="code">A1</code> are different identifiers. Keywords such as <code class="keyw">quit</code> must not be used as identifiers. You will see more keywords in the following sections.</p>
<p>In the remaining part of this manual we will ignore the difference between variables, their names (identifiers), and the objects they point to. It may be useful to think from time to time about what is really meant by terms such as "the integer <code class="code">w</code>".</p>
<p>There are some predefined variables coming with <strong class="pkg">GAP</strong>. Many of them you will find in the remaining chapters of this manual, since functions are also referred to via identifiers.</p>
<p>You can get an overview of <em>all</em> <strong class="pkg">GAP</strong> variables by entering <code class="code">NamesGVars()</code>. Many of these are predefined. If you are interested in the variables you have defined yourself in the current <strong class="pkg">GAP</strong> session, you can enter <code class="code">NamesUserGVars()</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">NamesUserGVars();</span>
[ "a", "w" ]
</pre></div>
<p>This seems to be the right place to state the following rule: The name of every global variable in the <strong class="pkg">GAP</strong> library starts with a <em>capital letter</em>. Thus if you choose only names starting with a small letter for your own variables you will not attempt to overwrite any predefined variable. (Note that most of the predefined variables are read-only, and trying to change their values will result in an error message.)</p>
<p>There are some further interesting variables one of which will be introduced now.</p>
<p>Whenever <strong class="pkg">GAP</strong> returns an object by printing it on the next line this object is assigned to the variable <code class="code">last</code>. So if you computed</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">(9 - 7) * (5 + 6);</span>
22
</pre></div>
<p>and forgot to assign the object to the variable <code class="code">a</code> for further use, you can still do it by the following assignment.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a:= last;</span>
22
</pre></div>
<p>Moreover there are variables <code class="code">last2</code> and <code class="code">last3</code>, you can guess their values.</p>
<p>In this section you have seen how to assign objects to variables. These objects can later be accessed through the name of the variable, its identifier. You have also encountered the useful concept of the <code class="code">last</code> variables storing the latest returned objects. And you have learned that a double semicolon prevents the result of a statement from being printed.</p>
<p><a id="X7F36AE347AD3CDEC" name="X7F36AE347AD3CDEC"></a></p>
<h4>2.6 <span class="Heading">Objects vs. Elements</span></h4>
<p>In the last section we mentioned that every object is given a certain place in memory by the <strong class="pkg">GAP</strong> storage manager (although that place may change in the course of a <strong class="pkg">GAP</strong> session). In this sense, objects at different places in memory are never equal, and if the object pointed to by the variable <code class="code">a</code> (to be more precise, the variable with identifier <code class="code">a</code>) is equal to the object pointed to by the variable <code class="code">b</code>, then we should better say that they are not only equal but <em>identical</em>. <strong class="pkg">GAP</strong> provides the function <code class="func">IsIdenticalObj</code> (<a href="../../doc/ref/chap12.html#X7961183378DFB902"><span class="RefLink">Reference: IsIdenticalObj</span></a>) to test whether this is the case.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a:= (1,2);; IsIdenticalObj( a, a );</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">b:= (1,2);; IsIdenticalObj( a, b );</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">b:= a;; IsIdenticalObj( a, b );</span>
true
</pre></div>
<p>As the above example indicates, <strong class="pkg">GAP</strong> objects <var class="Arg">a</var> and <var class="Arg">b</var> can be unequal although they are equal from a mathematical point of view, i.e., although we should have <var class="Arg">a</var> = <var class="Arg">b</var>. It may be that the objects <var class="Arg">a</var> and <var class="Arg">b</var> are stored in different places in memory, or it may be that we have an equivalence relation defined on the set of objects under which <var class="Arg">a</var> and <var class="Arg">b</var> belong to the same equivalence class. For example, if <span class="SimpleMath"><var class="Arg">a</var> = x^3</span> and <span class="SimpleMath"><var class="Arg">b</var> = x^{-5}</span> are words in the finitely presented group <span class="SimpleMath">⟨ x ∣ x^2 = 1 ⟩</span>, we would have <var class="Arg">a</var> = <var class="Arg">b</var> in that group.</p>
<p><strong class="pkg">GAP</strong> uses the equality operator <code class="code">=</code> to denote such a mathematical equality, <em>not</em> the identity of objects. Hence we often have <code class="code"><var class="Arg">a</var> = <var class="Arg">b</var></code> although <code class="code">IsIdenticalObj( <var class="Arg">a</var>, <var class="Arg">b</var> ) = false</code>. The operator <code class="code">=</code> defines an equivalence relation on the set of all <strong class="pkg">GAP</strong> objects, and we call the corresponding equivalence classes <em>elements</em>. Phrasing it differently, the same element may be represented by various <strong class="pkg">GAP</strong> objects.</p>
<p>Non-trivial examples of elements that are represented by different objects (objects that really look different, not ones that are merely stored in different memory places) will occur only when we will be considering composite objects such as lists or domains.</p>
<p><a id="X796E055D86856B42" name="X796E055D86856B42"></a></p>
<h4>2.7 <span class="Heading">About Functions</span></h4>
<p>A program written in the <strong class="pkg">GAP</strong> language is called a <em>function</em>. Functions are special <strong class="pkg">GAP</strong> objects. Most of them behave like mathematical functions. They are applied to objects and will return a new object depending on the input. The function <code class="func">Factorial</code> (<a href="../../doc/ref/chap16.html#X87665F748594BF29"><span class="RefLink">Reference: Factorial</span></a>), for example, can be applied to an integer and will return the factorial of this integer.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Factorial(17);</span>
355687428096000
</pre></div>
<p>Applying a function to arguments means to write the arguments in parentheses following the function. Several arguments are separated by commas, as for the function <code class="func">Gcd</code> (<a href="../../doc/ref/chap56.html#X7DE207718456F98F"><span class="RefLink">Reference: Gcd</span></a>) which computes the greatest common divisor of two integers.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Gcd(1234, 5678);</span>
2
</pre></div>
<p>There are other functions that do not return an object but only produce a side effect, for example changing one of their arguments. These functions are sometimes called procedures. The function <code class="func">Print</code> (<a href="../../doc/ref/chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">Reference: Print</span></a>) is only called for the side effect of printing something on the screen.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Print(1234, "\n");</span>
1234
</pre></div>
<p>In order to be able to compose arbitrary text with <code class="func">Print</code> (<a href="../../doc/ref/chap6.html#X7AFA64D97A1F39A3"><span class="RefLink">Reference: Print</span></a>), this function itself will not produce a line break after printing. Thus we had another newline character <code class="code">"\n"</code> printed to start a new line.</p>
<p>Some functions will both change an argument and return an object such as the function <code class="func">Sortex</code> (<a href="../../doc/ref/chap21.html#X87287FCA81E2B06A"><span class="RefLink">Reference: Sortex</span></a>) that sorts a list and returns the permutation of the list elements that it has performed. You will not understand right now what it means to change an object. We will return to this subject several times in the next sections.</p>
<p>A comfortable way to define a function yourself is the <em>maps-to</em> operator <code class="code">-></code> consisting of a minus sign and a greater sign with no whitespace between them. The function <code class="code">cubed</code> which maps a number to its cube is defined on the following line.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">cubed:= x -> x^3;</span>
function( x ) ... end
</pre></div>
<p>After the function has been defined, it can now be applied.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">cubed(5);</span>
125
</pre></div>
<p>More complicated functions, especially functions with more than one argument cannot be defined in this way. You will see how to write your own <strong class="pkg">GAP</strong> functions in Section <a href="chap4.html#X7D81680E7BB09F45"><span class="RefLink">4.1</span></a>.</p>
<p>In this section you have seen <strong class="pkg">GAP</strong> objects of type function. You have learned how to apply a function to arguments. This yields as result a new object or a side effect. A side effect may change an argument of the function. Moreover you have seen an easy way to define a function in <strong class="pkg">GAP</strong> with the maps-to operator.</p>
<p><a id="X7A7EADB77FF38BC9" name="X7A7EADB77FF38BC9"></a></p>
<h4>2.8 <span class="Heading">Help</span></h4>
<p>The content of the <strong class="pkg">GAP</strong> manuals is also available as on-line help. A <strong class="pkg">GAP</strong> session loads a long list of index entries. This typically contains all chapter and section headers, all names of documented functions, operations and so on, as well as some explicit index entries defined in the manuals.</p>
<p>The format of a query is as follows.</p>
<p><code class="code">?[<var class="Arg">book</var>:][?]<var class="Arg">topic</var></code></p>
<p>A simple example would be to type <code class="code">?help</code> at the <strong class="pkg">GAP</strong> prompt. If there is a single section with index entry <var class="Arg">topic</var> then this is displayed directly.</p>
<p>If there are several matches you get an overview like in the example below.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">?sets</span>
Help: several entries match this topic - type ?2 to get match [2]
[1] Tutorial: Sets
[2] Reference: Sets
[3] Reference: sets
[4] Reference: Sets of Subgroups
[5] Reference: setstabilizer
</pre></div>
<p><strong class="pkg">GAP</strong>'s manuals consist of several <em>books</em>, which are indicated before the colon in the list above. A help query can be restricted to one book by using the optional <var class="Arg">book</var>: part. For example <code class="code">?tut : sets</code> will display the first of these help sections. More precisely, the parts of the string <var class="Arg">book</var> which are separated by white space are interpreted as beginnings of the first words in the name of the book. Try <code class="code">?books</code> to see the list of available books and their names.</p>
<p>The search for a matching <var class="Arg">topic</var> (and optional <var class="Arg">book</var>) is done <em>case insensitively</em>. If there is another <code class="code">?</code> before the <var class="Arg">topic</var>, then a <em>substring search</em> for <var class="Arg">topic</var> is performed on all index entries. Otherwise the parts of <var class="Arg">topic</var> which are separated by white space are considered as <em>beginnings of the first words</em> in an index entry.</p>
<p>White space is normalized in the search string (and the index entries).</p>
<p><em>Examples.</em> All the following queries lead to the chapter of the reference manual which explains the use of <strong class="pkg">GAP</strong>'s help system in more detail.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">?Reference: The Help System</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">? REF : t h s</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">?ref:? help system </span>
</pre></div>
<p>The query <code class="code">??sets</code> shows all help sections in all books whose index entries contain the substring <code class="code">sets</code>.</p>
<p>As mentioned in the example above a complete list of commands for the help system is available in Section <code class="code">?Ref: The Help System</code> of the reference manual. In particular there are commands to browse through the help sections, see <code class="code">?Ref: Browsing through the Sections</code> and there is a way to influence the way <em>how</em> the help sections are displayed, see <code class="code">?Ref: SetHelpViewer</code>. For example you can use an external pager program, a Web browser, <code class="code">dvi</code>-previewer and/or <code class="code">pdf</code>-viewer for reading <strong class="pkg">GAP</strong>'s online help.</p>
<p><a id="X798515EF7D762548" name="X798515EF7D762548"></a></p>
<h4>2.9 <span class="Heading">Further Information introducing the System</span></h4>
<p>For large amounts of input data, it might be advisable to write your input first into a file, and then read this into <strong class="pkg">GAP</strong>; see <code class="func">Read</code> (<a href="../../doc/ref/chap9.html#X8373AC6B7D5F9167"><span class="RefLink">Reference: Read</span></a>), <code class="func">Edit</code> (<a href="../../doc/ref/chap6.html#X82E5859C8113BA4D"><span class="RefLink">Reference: Edit</span></a>) for this.</p>
<p>The definition of the <strong class="pkg">GAP</strong> syntax can be looked up in Chapter <a href="../../doc/ref/chap4.html#X7FE7C0C17E1ED118"><span class="RefLink">Reference: The Programming Language</span></a>. A complete list of command line editing facilities is found in Section <a href="../../doc/ref/chap6.html#X82234FD181899530"><span class="RefLink">Reference: Line Editing</span></a>. The break loop is described in Section <a href="../../doc/ref/chap6.html#X8593B49F8705B486"><span class="RefLink">Reference: Break Loops</span></a>.</p>
<p>Operators are explained in more detail in Sections <a href="../../doc/ref/chap4.html#X7BAFE9C1817253C6"><span class="RefLink">Reference: Expressions</span></a> and <a href="../../doc/ref/chap4.html#X7A274A1F8553B7E6"><span class="RefLink">Reference: Comparisons</span></a>. You will find more information about boolean values in Chapters <a href="../../doc/ref/chap20.html#X787B4AB77A2F5E14"><span class="RefLink">Reference: Booleans</span></a> and <a href="../../doc/ref/chap22.html#X7AC531DD79B6938E"><span class="RefLink">Reference: Boolean Lists</span></a>. Permutations are described in Chapter <a href="../../doc/ref/chap42.html#X80F808307A2D5AB8"><span class="RefLink">Reference: Permutations</span></a> and characters in Chapter <a href="../../doc/ref/chap27.html#X7D28329B7EDB8F47"><span class="RefLink">Reference: Strings and Characters</span></a>.</p>
<p>Variables and assignments are described in more detail in <a href="../../doc/ref/chap4.html#X7A4C2D0E7E286B4F"><span class="RefLink">Reference: Variables</span></a> and <a href="../../doc/ref/chap4.html#X7E6A50307F4D3FAE"><span class="RefLink">Reference: Assignments</span></a>. A complete list of keywords is contained in <a href="../../doc/ref/chap4.html#X87506BDC7D5F789E"><span class="RefLink">Reference: Keywords</span></a>.</p>
<p>More about functions can be found in <a href="../../doc/ref/chap4.html#X78C70489791FDF43"><span class="RefLink">Reference: Function Calls</span></a> and <a href="../../doc/ref/chap4.html#X825803DE78251DA6"><span class="RefLink">Reference: Procedure Calls</span></a>.</p>
<div class="chlinkprevnextbot"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap1.html">[Previous Chapter]</a> <a href="chap3.html">[Next Chapter]</a> </div>
<div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
<hr />
<p class="foot">generated by <a href="http://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
</body>
</html>
|