/usr/share/gap/doc/ref/chap4.html is in gap-doc 4r6p5-3.
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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 | <?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 (ref) - Chapter 4: The Programming Language</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="chap4" 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="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</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="chap3.html">[Previous Chapter]</a> <a href="chap5.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap4_mj.html">[MathJax on]</a></p>
<p><a id="X7FE7C0C17E1ED118" name="X7FE7C0C17E1ED118"></a></p>
<div class="ChapSects"><a href="chap4.html#X7FE7C0C17E1ED118">4 <span class="Heading">The Programming Language</span></a>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7B5FF6827DFBDF20">4.1 <span class="Heading">Language Overview</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X80A85A707B6F4BE7">4.2 <span class="Heading">Lexical Structure</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7E90E6607F4E4943">4.3 <span class="Heading">Symbols</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7C53CEFC8641B919">4.4 <span class="Heading">Whitespaces</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X87506BDC7D5F789E">4.5 <span class="Heading">Keywords</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X860313A179A5163F">4.6 <span class="Heading">Identifiers</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X85CF993B7D19F2C4">4.6-1 IsValidIdentifier</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7BAFE9C1817253C6">4.7 <span class="Heading">Expressions</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7A4C2D0E7E286B4F">4.8 <span class="Heading">Variables</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X842B89D4860FD5DB">4.8-1 IsBound</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X7BABB3E77F52626C">4.8-2 Unbind</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X816FBEEA85782EC2">4.9 <span class="Heading">More About Global Variables</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X7CD3523B84744EB2">4.9-1 IsReadOnlyGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X850CE44478254F27">4.9-2 MakeReadOnlyGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X832AAF13861968BE">4.9-3 MakeReadWriteGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X84BB4B1E872849FF">4.9-4 ValueGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X823D4BC378395B32">4.9-5 IsBoundGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X829A5F0E811F77D3">4.9-6 UnbindGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X7D39D3E17CF49F5B">4.9-7 BindGlobal</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X876A6EB68745A510">4.9-8 NamesGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X7E604AF579A7BC92">4.9-9 NamesSystemGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X870169447AF490D8">4.9-10 NamesUserGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X798433307E62DCBA">4.9-11 TemporaryGlobalVarName</a></span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X875BA7C58175DEFB">4.10 <span class="Heading">Namespaces for <strong class="pkg">GAP</strong> packages</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X78C70489791FDF43">4.11 <span class="Heading">Function Calls</span></a>
</span>
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X80B93A9C7E0A57F4">4.11-1 <span class="Heading">Function Call With Arguments</span></a>
</span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4.html#X867D54987EF86D1D">4.11-2 <span class="Heading">Function Call With Options</span></a>
</span>
</div></div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7A274A1F8553B7E6">4.12 <span class="Heading">Comparisons</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7B66C8707B5DE10A">4.13 <span class="Heading">Arithmetic Operators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X8543285D87361BE6">4.14 <span class="Heading">Statements</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7E6A50307F4D3FAE">4.15 <span class="Heading">Assignments</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X825803DE78251DA6">4.16 <span class="Heading">Procedure Calls</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X875000188622700D">4.17 <span class="Heading">If</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X87AA46408783383F">4.18 <span class="Heading">While</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X8295CBF47FAA05C9">4.19 <span class="Heading">Repeat</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X78783E777867638A">4.20 <span class="Heading">For</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7B60C6127E183021">4.21 <span class="Heading">Break</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X7CCBA2247AA366BD">4.22 <span class="Heading">Continue</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X815F71EA7BC0EB6F">4.23 <span class="Heading">Function</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4.html#X812C6ABC7A182E9E">4.24 <span class="Heading">Return (With or without Value)</span></a>
</span>
</div>
</div>
<h3>4 <span class="Heading">The Programming Language</span></h3>
<p>This chapter describes the <strong class="pkg">GAP</strong> programming language. It should allow you in principle to predict the result of each and every input. In order to know what we are talking about, we first have to look more closely at the process of interpretation and the various representations of data involved.</p>
<p><a id="X7B5FF6827DFBDF20" name="X7B5FF6827DFBDF20"></a></p>
<h4>4.1 <span class="Heading">Language Overview</span></h4>
<p>First we have the input to <strong class="pkg">GAP</strong>, given as a string of characters. How those characters enter <strong class="pkg">GAP</strong> is operating system dependent, e.g., they might be entered at a terminal, pasted with a mouse into a window, or read from a file. The mechanism does not matter. This representation of expressions by characters is called the <em>external representation</em> of the expression. Every expression has at least one external representation that can be entered to get exactly this expression.</p>
<p>The input, i.e., the external representation, is transformed in a process called <em>reading</em> to an internal representation. At this point the input is analyzed and inputs that are not legal external representations, according to the rules given below, are rejected as errors. Those rules are usually called the <em>syntax</em> of a programming language.</p>
<p>The internal representation created by reading is called either an <em>expression</em> or a <em>statement</em>. Later we will distinguish between those two terms. However for now we will use them interchangeably. The exact form of the internal representation does not matter. It could be a string of characters equal to the external representation, in which case the reading would only need to check for errors. It could be a series of machine instructions for the processor on which <strong class="pkg">GAP</strong> is running, in which case the reading would more appropriately be called compilation. It is in fact a tree-like structure.</p>
<p>After the input has been read it is again transformed in a process called <em>evaluation</em> or <em>execution</em>. Later we will distinguish between those two terms too, but for the moment we will use them interchangeably. The name hints at the nature of this process, it replaces an expression with the value of the expression. This works recursively, i.e., to evaluate an expression first the subexpressions are evaluated and then the value of the expression is computed from those values according to rules given below. Those rules are usually called the <em>semantics</em> of a programming language.</p>
<p>The result of the evaluation is, not surprisingly, called a <em>value</em>. Again the form in which such a value is represented internally does not matter. It is in fact a tree-like structure again.</p>
<p>The last process is called <em>printing</em>. It takes the value produced by the evaluation and creates an external representation, i.e., a string of characters again. What you do with this external representation is up to you. You can look at it, paste it with the mouse into another window, or write it to a file.</p>
<p>Lets look at an example to make this more clear. Suppose you type in the following string of 8 characters</p>
<div class="example"><pre>
1 + 2 * 3;
</pre></div>
<p><strong class="pkg">GAP</strong> takes this external representation and creates a tree-like internal representation, which we can picture as follows</p>
<div class="example"><pre>
+
/ \
1 *
/ \
2 3
</pre></div>
<p>This expression is then evaluated. To do this <strong class="pkg">GAP</strong> first evaluates the right subexpression <code class="code">2*3</code>. Again, to do this <strong class="pkg">GAP</strong> first evaluates its subexpressions 2 and 3. However they are so simple that they are their own value, we say that they are self-evaluating. After this has been done, the rule for <code class="code">*</code> tells us that the value is the product of the values of the two subexpressions, which in this case is clearly 6. Combining this with the value of the left operand of the <code class="code">+</code>, which is self-evaluating, too, gives us the value of the whole expression 7. This is then printed, i.e., converted into the external representation consisting of the single character <code class="code">7</code>.</p>
<p>In this fashion we can predict the result of every input when we know the syntactic rules that govern the process of reading and the semantic rules that tell us for every expression how its value is computed in terms of the values of the subexpressions. The syntactic rules are given in sections <a href="chap4.html#X80A85A707B6F4BE7"><span class="RefLink">4.2</span></a>, <a href="chap4.html#X7E90E6607F4E4943"><span class="RefLink">4.3</span></a>, <a href="chap4.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>, <a href="chap4.html#X87506BDC7D5F789E"><span class="RefLink">4.5</span></a>, and <a href="chap4.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>, the semantic rules are given in sections <a href="chap4.html#X7BAFE9C1817253C6"><span class="RefLink">4.7</span></a>, <a href="chap4.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>, <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>, <a href="chap4.html#X7A274A1F8553B7E6"><span class="RefLink">4.12</span></a>, <a href="chap4.html#X7B66C8707B5DE10A"><span class="RefLink">4.13</span></a>, <a href="chap4.html#X8543285D87361BE6"><span class="RefLink">4.14</span></a>, <a href="chap4.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15</span></a>, <a href="chap4.html#X825803DE78251DA6"><span class="RefLink">4.16</span></a>, <a href="chap4.html#X875000188622700D"><span class="RefLink">4.17</span></a>, <a href="chap4.html#X87AA46408783383F"><span class="RefLink">4.18</span></a>, <a href="chap4.html#X8295CBF47FAA05C9"><span class="RefLink">4.19</span></a>, <a href="chap4.html#X78783E777867638A"><span class="RefLink">4.20</span></a>, <a href="chap4.html#X815F71EA7BC0EB6F"><span class="RefLink">4.23</span></a>, and the chapters describing the individual data types.</p>
<p><a id="X80A85A707B6F4BE7" name="X80A85A707B6F4BE7"></a></p>
<h4>4.2 <span class="Heading">Lexical Structure</span></h4>
<p>Most input of <strong class="pkg">GAP</strong> consists of sequences of the following characters.</p>
<p>Digits, uppercase and lowercase letters, <strong class="button">Space</strong>, <strong class="button">Tab</strong>, <strong class="button">Newline</strong>, <strong class="button">Return</strong> and the special characters</p>
<div class="example"><pre>
" ` ( ) * + , - #
. / : ; < = > ~
[ \ ] ^ _ { } !
</pre></div>
<p>It is possible to use other characters in identifiers by escaping them with backslashes, but we do not recommend to use this feature. Inside strings (see section <a href="chap4.html#X7E90E6607F4E4943"><span class="RefLink">4.3</span></a> and chapter <a href="chap27.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>) and comments (see <a href="chap4.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>) the full character set supported by the computer is allowed.</p>
<p><a id="X7E90E6607F4E4943" name="X7E90E6607F4E4943"></a></p>
<h4>4.3 <span class="Heading">Symbols</span></h4>
<p>The process of reading, i.e., of assembling the input into expressions, has a subprocess, called <em>scanning</em>, that assembles the characters into symbols. A <em>symbol</em> is a sequence of characters that form a lexical unit. The set of symbols consists of keywords, identifiers, strings, integers, and operator and delimiter symbols.</p>
<p>A <em>keyword</em> is a reserved word (see <a href="chap4.html#X87506BDC7D5F789E"><span class="RefLink">4.5</span></a>). An <em>identifier</em> is a sequence of letters, digits and underscores (or other characters escaped by backslashes) that contains at least one non-digit and is not a keyword (see <a href="chap4.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>). An integer is a sequence of digits (see <a href="chap14.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>), possibly prepended by <code class="code">-</code> and <code class="code">+</code> sign characters. A <em>string</em> is a sequence of arbitrary characters enclosed in double quotes (see <a href="chap27.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>).</p>
<p>Operator and delimiter symbols are</p>
<div class="example"><pre>
+ - * / ^ ~ !.
= <> < <= > >= ![
:= . .. -> , ; !{
[ ] { } ( ) :
</pre></div>
<p>Note also that during the process of scanning all whitespace is removed (see <a href="chap4.html#X7C53CEFC8641B919"><span class="RefLink">4.4</span></a>).</p>
<p><a id="X7C53CEFC8641B919" name="X7C53CEFC8641B919"></a></p>
<h4>4.4 <span class="Heading">Whitespaces</span></h4>
<p>The characters <strong class="button">Space</strong>, <strong class="button">Tab</strong>, <strong class="button">Newline</strong>, and <strong class="button">Return</strong> are called <em>whitespace characters</em>. Whitespace is used as necessary to separate lexical symbols, such as integers, identifiers, or keywords. For example <code class="code">Thorondor</code> is a single identifier, while <code class="code">Th or ondor</code> is the keyword <code class="keyw">or</code> between the two identifiers <code class="code">Th</code> and <code class="code">ondor</code>. Whitespace may occur between any two symbols, but not within a symbol. Two or more adjacent whitespace characters are equivalent to a single whitespace. Apart from the role as separator of symbols, whitespace characters are otherwise insignificant. Whitespace characters may also occur inside a string, where they are significant. Whitespace characters should also be used freely for improved readability.</p>
<p>A <em>comment</em> starts with the character <code class="code">#</code>, which is sometimes called sharp or hatch, and continues to the end of the line on which the comment character appears. The whole comment, including <code class="code">#</code> and the <strong class="button">Newline</strong> character is treated as a single whitespace. Inside a string, the comment character <code class="code">#</code> loses its role and is just an ordinary character.</p>
<p>For example, the following statement</p>
<div class="example"><pre>
if i<0 then a:=-i;else a:=i;fi;
</pre></div>
<p>is equivalent to</p>
<div class="example"><pre>
if i < 0 then # if i is negative
a := -i; # take its additive inverse
else # otherwise
a := i; # take itself
fi;
</pre></div>
<p>(which by the way shows that it is possible to write superfluous comments). However the first statement is <em>not</em> equivalent to</p>
<div class="example"><pre>
ifi<0thena:=-i;elsea:=i;fi;
</pre></div>
<p>since the keyword <code class="keyw">if</code> must be separated from the identifier <code class="code">i</code> by a whitespace, and similarly <code class="keyw">then</code> and <code class="code">a</code>, and <code class="keyw">else</code> and <code class="code">a</code> must be separated.</p>
<p><a id="X87506BDC7D5F789E" name="X87506BDC7D5F789E"></a></p>
<h4>4.5 <span class="Heading">Keywords</span></h4>
<p><em>Keywords</em> are reserved words that are used to denote special operations or are part of statements. They must not be used as identifiers. The list of keywords is contained in the <code class="code">GAPInfo.Keywords</code> component of the <code class="code">GAPInfo</code> record (see <a href="chap3.html#X8354754E7935F935"><span class="RefLink">3.5-1</span></a>). We will show how to print it in a nice table, demonstrating at the same time some list manipulation techniques:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">keys:=SortedList( GAPInfo.Keywords );; l:=Length( keys );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">arr:= List( [ 0 .. Int( l/4 )-1 ], i-> keys{ 4*i + [ 1 .. 4 ] } );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">if l mod 4 <> 0 then Add( arr, keys{[ 4*Int(l/4) + 1 .. l ]} ); fi;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Length( keys ); PrintArray( arr );</span>
32
[ [ Assert, Info, IsBound, QUIT ],
[ TryNextMethod, Unbind, and, break ],
[ continue, do, elif, else ],
[ end, false, fi, for ],
[ function, if, in, local ],
[ mod, not, od, or ],
[ quit, rec, repeat, return ],
[ then, true, until, while ] ]
</pre></div>
<p>Note that (almost) all keywords are written in lowercase and that they are case sensitive. For example <code class="keyw">else</code> is a keyword; <code class="code">Else</code>, <code class="code">eLsE</code>, <code class="code">ELSE</code> and so forth are ordinary identifiers. Keywords must not contain whitespace, for example <code class="code">el if</code> is not the same as <code class="keyw">elif</code>.</p>
<p><em>Note</em>: Several tokens from the list of keywords above may appear to be normal identifiers representing functions or literals of various kinds but are actually implemented as keywords for technical reasons. The only consequence of this is that those identifiers cannot be re-assigned, and do not actually have function objects bound to them, which could be assigned to other variables or passed to functions. These keywords are <code class="keyw">true</code>, <code class="keyw">false</code>, <code class="func">Assert</code> (<a href="chap7.html#X830E443284780FB9"><span class="RefLink">7.5-3</span></a>), <code class="func">IsBound</code> (<a href="chap4.html#X842B89D4860FD5DB"><span class="RefLink">4.8-1</span></a>), <code class="func">Unbind</code> (<a href="chap4.html#X7BABB3E77F52626C"><span class="RefLink">4.8-2</span></a>), <code class="func">Info</code> (<a href="chap7.html#X864E4B6886E2697D"><span class="RefLink">7.4-5</span></a>) and <code class="func">TryNextMethod</code> (<a href="chap78.html#X7EED949B83046A7F"><span class="RefLink">78.4-1</span></a>).</p>
<p><a id="X860313A179A5163F" name="X860313A179A5163F"></a></p>
<h4>4.6 <span class="Heading">Identifiers</span></h4>
<p>An <em>identifier</em> is used to refer to a variable (see <a href="chap4.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>). An identifier usually consists of letters, digits, underscores <code class="code">_</code>, and "at"-characters <code class="code">@</code>, and must contain at least one non-digit. An identifier is terminated by the first character not in this class. Note that the "at"-character <code class="code">@</code> is used to implement namespaces, see Section <a href="chap4.html#X875BA7C58175DEFB"><span class="RefLink">4.10</span></a> for details.</p>
<p>Examples of valid identifiers are</p>
<div class="example"><pre>
a foo aLongIdentifier
hello Hello HELLO
x100 100x _100
some_people_prefer_underscores_to_separate_words
WePreferMixedCaseToSeparateWords
abc@def
</pre></div>
<p>Note that case is significant, so the three identifiers in the second line are distinguished.</p>
<p>The backslash <code class="code">\</code> can be used to include other characters in identifiers; a backslash followed by a character is equivalent to the character, except that this escape sequence is considered to be an ordinary letter. For example</p>
<div class="example"><pre>
G\(2\,5\)
</pre></div>
<p>is an identifier, not a call to a function <code class="code">G</code>.</p>
<p>An identifier that starts with a backslash is never a keyword, so for example <code class="code">\*</code> and <code class="code">\mod</code> are identifiers.</p>
<p>The length of identifiers is not limited, however only the first <span class="SimpleMath">1023</span> characters are significant. The escape sequence <code class="code">\</code><strong class="button">newline</strong> is ignored, making it possible to split long identifiers over multiple lines.</p>
<p><a id="X85CF993B7D19F2C4" name="X85CF993B7D19F2C4"></a></p>
<h5>4.6-1 IsValidIdentifier</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsValidIdentifier</code>( <var class="Arg">str</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if the string <var class="Arg">str</var> would form a valid identifier consisting of letters, digits and underscores; otherwise it returns <code class="keyw">false</code>. It does not check whether <var class="Arg">str</var> contains characters escaped by a backslash <code class="code">\</code>.</p>
<p>Note that the "at"-character is used to implement namespaces for global variables in packages. See <a href="chap4.html#X875BA7C58175DEFB"><span class="RefLink">4.10</span></a> for details.</p>
<p><a id="X7BAFE9C1817253C6" name="X7BAFE9C1817253C6"></a></p>
<h4>4.7 <span class="Heading">Expressions</span></h4>
<p>An <em>expression</em> is a construct that evaluates to a value. Syntactic constructs that are executed to produce a side effect and return no value are called <em>statements</em> (see <a href="chap4.html#X8543285D87361BE6"><span class="RefLink">4.14</span></a>). Expressions appear as right hand sides of assignments (see <a href="chap4.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15</span></a>), as actual arguments in function calls (see <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>), and in statements.</p>
<p>Note that an expression is not the same as a value. For example <code class="code">1 + 11</code> is an expression, whose value is the integer 12. The external representation of this integer is the character sequence <code class="code">12</code>, i.e., this sequence is output if the integer is printed. This sequence is another expression whose value is the integer <span class="SimpleMath">12</span>. The process of finding the value of an expression is done by the interpreter and is called the <em>evaluation</em> of the expression.</p>
<p>Variables, function calls, and integer, permutation, string, function, list, and record literals (see <a href="chap4.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>, <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>, <a href="chap14.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>, <a href="chap42.html#X80F808307A2D5AB8"><span class="RefLink">42</span></a>, <a href="chap27.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>, <a href="chap4.html#X815F71EA7BC0EB6F"><span class="RefLink">4.23</span></a>, <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>, <a href="chap29.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a>), are the simplest cases of expressions.</p>
<p>Expressions, for example the simple expressions mentioned above, can be combined with the operators to form more complex expressions. Of course those expressions can then be combined further with the operators to form even more complex expressions. The <em>operators</em> fall into three classes. The <em>comparisons</em> are <code class="code">=</code>, <code class="code"><></code>, <code class="code"><</code>, <code class="code"><=</code>, <code class="code">></code>, <code class="code">>=</code>, and <code class="keyw">in</code> (see <a href="chap4.html#X7A274A1F8553B7E6"><span class="RefLink">4.12</span></a> and <a href="chap30.html#X82D39CF980FDBFFA"><span class="RefLink">30.6</span></a>). The <em>arithmetic operators</em> are <code class="code">+</code>, <code class="code">-</code>, <code class="code">*</code>, <code class="code">/</code>, <code class="keyw">mod</code>, and <code class="code">^</code> (see <a href="chap4.html#X7B66C8707B5DE10A"><span class="RefLink">4.13</span></a>). The <em>logical operators</em> are <code class="keyw">not</code>, <code class="keyw">and</code>, and <code class="keyw">or</code> (see <a href="chap20.html#X79AD41A185FD7213"><span class="RefLink">20.4</span></a>).</p>
<p>The following example shows a very simple expression with value 4 and a more complex expression.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2;</span>
4
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2 + 9 = Fibonacci(7) and Fibonacci(13) in Primes;</span>
true
</pre></div>
<p>For the precedence of operators, see <a href="chap4.html#X7A274A1F8553B7E6"><span class="RefLink">4.12</span></a>.</p>
<p><a id="X7A4C2D0E7E286B4F" name="X7A4C2D0E7E286B4F"></a></p>
<h4>4.8 <span class="Heading">Variables</span></h4>
<p>A <em>variable</em> is a location in a <strong class="pkg">GAP</strong> program that points to a value. We say the variable is <em>bound</em> to this value. If a variable is evaluated it evaluates to this value.</p>
<p>Initially an ordinary variable is not bound to any value. The variable can be bound to a value by <em>assigning</em> this value to the variable (see <a href="chap4.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15</span></a>). Because of this we sometimes say that a variable that is not bound to any value has no assigned value. Assignment is in fact the only way by which a variable, which is not an argument of a function, can be bound to a value. After a variable has been bound to a value an assignment can also be used to bind the variable to another value.</p>
<p>A special class of variables is the class of <em>arguments</em> of functions. They behave similarly to other variables, except they are bound to the value of the actual arguments upon a function call (see <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>).</p>
<p>Each variable has a name that is also called its <em>identifier</em>. This is because in a given scope an identifier identifies a unique variable (see <a href="chap4.html#X860313A179A5163F"><span class="RefLink">4.6</span></a>). A <em>scope</em> is a lexical part of a program text. There is the <em>global scope</em> that encloses the entire program text, and there are local scopes that range from the <code class="keyw">function</code> keyword, denoting the beginning of a function definition, to the corresponding <code class="keyw">end</code> keyword. A <em>local scope</em> introduces new variables, whose identifiers are given in the formal argument list and the <code class="keyw">local</code> declaration of the function (see <a href="chap4.html#X815F71EA7BC0EB6F"><span class="RefLink">4.23</span></a>). Usage of an identifier in a program text refers to the variable in the innermost scope that has this identifier as its name. Because this mapping from identifiers to variables is done when the program is read, not when it is executed, <strong class="pkg">GAP</strong> is said to have <em>lexical scoping</em>. The following example shows how one identifier refers to different variables at different points in the program text.</p>
<div class="example"><pre>
g := 0; # global variable g
x := function ( a, b, c )
local y;
g := c; # c refers to argument c of function x
y := function ( y )
local d, e, f;
d := y; # y refers to argument y of function y
e := b; # b refers to argument b of function x
f := g; # g refers to global variable g
return d + e + f;
end;
return y( a ); # y refers to local y of function x
end;
</pre></div>
<p>It is important to note that the concept of a variable in <strong class="pkg">GAP</strong> is quite different from the concept of a variable in most compiled programming languages.</p>
<p>In those languages a variable denotes a block of memory. The value of the variable is stored in this block. So in those languages two variables can have the same value, but they can never have identical values, because they denote different blocks of memory. Note that some languages have the concept of a reference argument. It seems as if such an argument and the variable used in the actual function call have the same value, since changing the argument's value also changes the value of the variable used in the actual function call. But this is not so; the reference argument is actually a pointer to the variable used in the actual function call, and it is the compiler that inserts enough magic to make the pointer invisible. In order for this to work the compiler needs enough information to compute the amount of memory needed for each variable in a program, which is readily available in the declarations.</p>
<p>In <strong class="pkg">GAP</strong> on the other hand each variable just points to a value, and different variables can share the same value.</p>
<p><a id="X842B89D4860FD5DB" name="X842B89D4860FD5DB"></a></p>
<h5>4.8-1 IsBound</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsBound</code>( <var class="Arg">ident</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p><code class="func">IsBound</code> returns <code class="keyw">true</code> if the variable <var class="Arg">ident</var> points to a value, and <code class="keyw">false</code> otherwise.</p>
<p>For records and lists <code class="func">IsBound</code> can be used to check whether components or entries, respectively, are bound (see Chapters <a href="chap29.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a> and <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>).</p>
<p><a id="X7BABB3E77F52626C" name="X7BABB3E77F52626C"></a></p>
<h5>4.8-2 Unbind</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Unbind</code>( <var class="Arg">ident</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>deletes the identifier <var class="Arg">ident</var>. If there is no other variable pointing to the same value as <var class="Arg">ident</var> was, this value will be removed by the next garbage collection. Therefore <code class="func">Unbind</code> can be used to get rid of unwanted large objects.</p>
<p>For records and lists <code class="func">Unbind</code> can be used to delete components or entries, respectively (see Chapters <a href="chap29.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a> and <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>).</p>
<p><a id="X816FBEEA85782EC2" name="X816FBEEA85782EC2"></a></p>
<h4>4.9 <span class="Heading">More About Global Variables</span></h4>
<p>The vast majority of variables in <strong class="pkg">GAP</strong> are defined at the outer level (the global scope). They are used to access functions and other objects created either in the <strong class="pkg">GAP</strong> library or packages or in the user's code.</p>
<p>Note that for packages there is a mechanism to implement package local namespaces on top of this global namespace. See Section <a href="chap4.html#X875BA7C58175DEFB"><span class="RefLink">4.10</span></a> for details.</p>
<p>Certain special facilities are provided for manipulating global variables which are not available for other types of variable (such as local variables or function arguments).</p>
<p>First, such variables may be marked <em>read-only</em>. In which case attempts to change them will fail. Most of the global variables defined in the <strong class="pkg">GAP</strong> library are so marked.</p>
<p>Second, a group of functions are supplied for accessing and altering the values assigned to global variables. Use of these functions differs from the use of assignment, <code class="func">Unbind</code> (<a href="chap4.html#X7BABB3E77F52626C"><span class="RefLink">4.8-2</span></a>) and <code class="func">IsBound</code> (<a href="chap4.html#X842B89D4860FD5DB"><span class="RefLink">4.8-1</span></a>) statements, in two ways. First, these functions always affect global variables, even if local variables of the same names exist. Second, the variable names are passed as strings, rather than being written directly into the statements.</p>
<p>Note that the functions <code class="func">NamesGVars</code> (<a href="chap4.html#X876A6EB68745A510"><span class="RefLink">4.9-8</span></a>), <code class="func">NamesSystemGVars</code> (<a href="chap4.html#X7E604AF579A7BC92"><span class="RefLink">4.9-9</span></a>), <code class="func">NamesUserGVars</code> (<a href="chap4.html#X870169447AF490D8"><span class="RefLink">4.9-10</span></a>), and <code class="func">TemporaryGlobalVarName</code> (<a href="chap4.html#X798433307E62DCBA"><span class="RefLink">4.9-11</span></a>) deal with the <em>global namespace</em>.</p>
<p><a id="X7CD3523B84744EB2" name="X7CD3523B84744EB2"></a></p>
<h5>4.9-1 IsReadOnlyGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsReadOnlyGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if the global variable named by the string <var class="Arg">name</var> is read-only and <code class="keyw">false</code> otherwise (the default).</p>
<p><a id="X850CE44478254F27" name="X850CE44478254F27"></a></p>
<h5>4.9-2 MakeReadOnlyGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeReadOnlyGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>marks the global variable named by the string <var class="Arg">name</var> as read-only.</p>
<p>A warning is given if <var class="Arg">name</var> has no value bound to it or if it is already read-only.</p>
<p><a id="X832AAF13861968BE" name="X832AAF13861968BE"></a></p>
<h5>4.9-3 MakeReadWriteGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ MakeReadWriteGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>marks the global variable named by the string <var class="Arg">name</var> as read-write.</p>
<p>A warning is given if <var class="Arg">name</var> is already read-write.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 17;</span>
17
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 15;</span>
15
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadOnlyGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
Variable: 'xx' is read only
not in any function
Entering break read-eval-print loop ...
you can 'quit;' to quit to outer loop, or
you can 'return;' after making it writable to continue
<span class="GAPbrkprompt">brk></span> <span class="GAPinput">quit;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadWriteGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
</pre></div>
<p><a id="X84BB4B1E872849FF" name="X84BB4B1E872849FF"></a></p>
<h5>4.9-4 ValueGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ValueGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns the value currently bound to the global variable named by the string <var class="Arg">name</var>. An error is raised if no value is currently bound.</p>
<p><a id="X823D4BC378395B32" name="X823D4BC378395B32"></a></p>
<h5>4.9-5 IsBoundGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsBoundGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns <code class="keyw">true</code> if a value currently bound to the global variable named by the string <var class="Arg">name</var> and <code class="keyw">false</code> otherwise.</p>
<p><a id="X829A5F0E811F77D3" name="X829A5F0E811F77D3"></a></p>
<h5>4.9-6 UnbindGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ UnbindGlobal</code>( <var class="Arg">name</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>removes any value currently bound to the global variable named by the string <var class="Arg">name</var>. Nothing is returned.</p>
<p>A warning is given if <var class="Arg">name</var> was not bound. The global variable named by <var class="Arg">name</var> must be writable, otherwise an error is raised.</p>
<p><a id="X7D39D3E17CF49F5B" name="X7D39D3E17CF49F5B"></a></p>
<h5>4.9-7 BindGlobal</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BindGlobal</code>( <var class="Arg">name</var>, <var class="Arg">val</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>sets the global variable named by the string <var class="Arg">name</var> to the value <var class="Arg">val</var>, provided it is writable, and makes it read-only. If <var class="Arg">name</var> already had a value, a warning message is printed.</p>
<p>This is intended to be the normal way to create and set "official" global variables (such as operations and filters).</p>
<p>Caution should be exercised in using these functions, especially <code class="func">BindGlobal</code> and <code class="func">UnbindGlobal</code> (<a href="chap4.html#X829A5F0E811F77D3"><span class="RefLink">4.9-6</span></a>) as unexpected changes in global variables can be very confusing for the user.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">xx := 16;</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">ValueGlobal("xx");</span>
16
<span class="GAPprompt">gap></span> <span class="GAPinput">IsBoundGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">BindGlobal("xx",17);</span>
#W BIND_GLOBAL: variable `xx' already has a value
<span class="GAPprompt">gap></span> <span class="GAPinput">xx;</span>
17
<span class="GAPprompt">gap></span> <span class="GAPinput">IsReadOnlyGlobal("xx");</span>
true
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadWriteGlobal("xx");</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Unbind(xx);</span>
</pre></div>
<p><a id="X876A6EB68745A510" name="X876A6EB68745A510"></a></p>
<h5>4.9-8 NamesGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>) sorted (see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>) list of all the global variable names known to the system. This includes names of variables which were bound but have now been unbound and some other names which have never been bound but have become known to the system by various routes.</p>
<p><a id="X7E604AF579A7BC92" name="X7E604AF579A7BC92"></a></p>
<h5>4.9-9 NamesSystemGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesSystemGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable sorted list of all the global variable names created by the <strong class="pkg">GAP</strong> library when <strong class="pkg">GAP</strong> was started.</p>
<p><a id="X870169447AF490D8" name="X870169447AF490D8"></a></p>
<h5>4.9-10 NamesUserGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ NamesUserGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>This function returns an immutable sorted list of the global variable names created since the library was read, to which a value is currently bound.</p>
<p><a id="X798433307E62DCBA" name="X798433307E62DCBA"></a></p>
<h5>4.9-11 TemporaryGlobalVarName</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ TemporaryGlobalVarName</code>( [<var class="Arg">prefix</var>] )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns a string that can be used as the name of a global variable that is not bound at the time when <code class="func">TemporaryGlobalVarName</code> is called. The optional argument <var class="Arg">prefix</var> can specify a string with which the name of the global variable starts.</p>
<p><a id="X875BA7C58175DEFB" name="X875BA7C58175DEFB"></a></p>
<h4>4.10 <span class="Heading">Namespaces for <strong class="pkg">GAP</strong> packages</span></h4>
<p>As mentioned in Section <a href="chap4.html#X816FBEEA85782EC2"><span class="RefLink">4.9</span></a> above all global variables share a common namespace. This can relatively easily lead to name clashes, in particular when many <strong class="pkg">GAP</strong> packages are loaded at the same time. To give package code a way to have a package local namespace without breaking backward compatibility of the <strong class="pkg">GAP</strong> language, the following simple rule has been devised:</p>
<p>If in package code a global variable that ends with an "at"-character <code class="code">@</code> is accessed in any way, the name of the package is appended before accessing it. Here, "package code" refers to everything which is read with <code class="func">ReadPackage</code> (<a href="chap76.html#X870954577B27DCAB"><span class="RefLink">76.3-1</span></a>). As the name of the package the entry <code class="code">PackageName</code> in its <code class="file">PackageInfo.g</code> file is taken. As for all identifiers, this name is case sensitive.</p>
<p>For example, if the following is done in the code of a package with name <code class="code">xYz</code>:</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a@ := 12;</span>
</pre></div>
<p>Then actually the global variable <code class="code">a@xYz</code> is assigned. Further accesses to <code class="code">a@</code> within the package code will all be redirected to <code class="code">a@xYz</code>. This includes all the functions described in Section <a href="chap4.html#X816FBEEA85782EC2"><span class="RefLink">4.9</span></a> and indeed all the functions described Section <a href="chap79.html#X87E29BA57C8208A4"><span class="RefLink">79.17</span></a> like for example <code class="func">DeclareCategory</code> (<a href="chap79.html#X879DE2A17A6C6E92"><span class="RefLink">79.17-1</span></a>). Note that from code in the same package it is still possible to access the same global variable via <code class="code">a@xYz</code> explicitly.</p>
<p>All other code outside the package as well as interactive user input that wants to refer to that variable <code class="code">a@xYz</code> must do so explicitly by using <code class="code">a@xYz</code>.</p>
<p>Since in earlier releases of <strong class="pkg">GAP</strong> the "at"-character <code class="code">@</code> was not a legal character (without using backslashes), this small extension of the language does not break any old code.</p>
<p><a id="X78C70489791FDF43" name="X78C70489791FDF43"></a></p>
<h4>4.11 <span class="Heading">Function Calls</span></h4>
<p><a id="X80B93A9C7E0A57F4" name="X80B93A9C7E0A57F4"></a></p>
<h5>4.11-1 <span class="Heading">Function Call With Arguments</span></h5>
<p><code class="code"><var class="Arg">function-var</var>( [<var class="Arg">arg-expr</var>[, <var class="Arg">arg-expr</var>, ...]] )</code></p>
<p>The function call has the effect of calling the function <var class="Arg">function-var</var>. The precise semantics are as follows.</p>
<p>First <strong class="pkg">GAP</strong> evaluates the <var class="Arg">function-var</var>. Usually <var class="Arg">function-var</var> is a variable, and <strong class="pkg">GAP</strong> does nothing more than taking the value of this variable. It is allowed though that <var class="Arg">function-var</var> is a more complex expression, such as a reference to an element of a list (see Chapter <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>) <code class="code"><var class="Arg">list-var</var>[<var class="Arg">int-expr</var>]</code>, or to a component of a record (see Chapter <a href="chap29.html#X7AA1073C7E943DD7"><span class="RefLink">29</span></a>) <code class="code"><var class="Arg">record-var</var>.<var class="Arg">ident</var></code>. In any case <strong class="pkg">GAP</strong> tests whether the value is a function. If it is not, <strong class="pkg">GAP</strong> signals an error.</p>
<p>Next <strong class="pkg">GAP</strong> checks that the number of actual arguments <var class="Arg">arg-expr</var>s agrees with the number of <em>formal arguments</em> as given in the function definition. If they do not agree <strong class="pkg">GAP</strong> signals an error. An exception is the case when there is exactly one formal argument with the name <code class="code">arg</code>, in which case any number of actual arguments is allowed (see <a href="chap4.html#X815F71EA7BC0EB6F"><span class="RefLink">4.23</span></a> for examples).</p>
<p>Now <strong class="pkg">GAP</strong> allocates for each formal argument and for each <em>formal local</em> (that is, the identifiers in the <code class="keyw">local</code> declaration) a new variable. Remember that a variable is a location in a <strong class="pkg">GAP</strong> program that points to a value. Thus for each formal argument and for each formal local such a location is allocated.</p>
<p>Next the arguments <var class="Arg">arg-expr</var>s are evaluated, and the values are assigned to the newly created variables corresponding to the formal arguments. Of course the first value is assigned to the new variable corresponding to the first formal argument, the second value is assigned to the new variable corresponding to the second formal argument, and so on. However, <strong class="pkg">GAP</strong> does not make any guarantee about the order in which the arguments are evaluated. They might be evaluated left to right, right to left, or in any other order, but each argument is evaluated once. An exception again occurs if the function has only one formal argument with the name <code class="code">arg</code>. In this case the values of all the actual arguments are stored in a list and this list is assigned to the new variable corresponding to the formal argument <code class="code">arg</code>.</p>
<p>The new variables corresponding to the formal locals are initially not bound to any value. So trying to evaluate those variables before something has been assigned to them will signal an error.</p>
<p>Now the body of the function, which is a statement, is executed. If the identifier of one of the formal arguments or formal locals appears in the body of the function it refers to the new variable that was allocated for this formal argument or formal local, and evaluates to the value of this variable.</p>
<p>If during the execution of the body of the function a <code class="keyw">return</code> statement with an expression (see <a href="chap4.html#X812C6ABC7A182E9E"><span class="RefLink">4.24</span></a>) is executed, execution of the body is terminated and the value of the function call is the value of the expression of the <code class="keyw">return</code>. If during the execution of the body a <code class="keyw">return</code> statement without an expression is executed, execution of the body is terminated and the function call does not produce a value, in which case we call this call a procedure call (see <a href="chap4.html#X825803DE78251DA6"><span class="RefLink">4.16</span></a>). If the execution of the body completes without execution of a <code class="keyw">return</code> statement, the function call again produces no value, and again we talk about a procedure call.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Fibonacci( 11 );</span>
89
</pre></div>
<p>The above example shows a call to the function <code class="func">Fibonacci</code> (<a href="chap16.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>) with actual argument <code class="code">11</code>, the following one shows a call to the operation <code class="func">RightCosets</code> (<a href="chap39.html#X835F48248571364F"><span class="RefLink">39.7-2</span></a>) where the second actual argument is another function call.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">RightCosets( G, Intersection( U, V ) );;</span>
</pre></div>
<p><a id="X867D54987EF86D1D" name="X867D54987EF86D1D"></a></p>
<h5>4.11-2 <span class="Heading">Function Call With Options</span></h5>
<p><code class="code"><var class="Arg">function-var</var>( <var class="Arg">arg-expr</var>[, <var class="Arg">arg-expr</var>, ...][ : [ <var class="Arg">option-expr</var> [,<var class="Arg">option-expr</var>, ....]]])</code></p>
<p>As well as passing arguments to a function, providing the mathematical input to its calculation, it is sometimes useful to supply "hints" suggesting to <strong class="pkg">GAP</strong> how the desired result may be computed more quickly, or specifying a level of tolerance for random errors in a Monte Carlo algorithm.</p>
<p>Such hints may be supplied to a function-call <em>and to all subsidiary functions called from that call</em> using the options mechanism. Options are separated from the actual arguments by a colon <code class="code">:</code> and have much the same syntax as the components of a record expression. The one exception to this is that a component name may appear without a value, in which case the value <code class="keyw">true</code> is silently inserted.</p>
<p>The following example shows a call to <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) passing the options <code class="code">hard</code> (with the value <code class="keyw">true</code>) and <code class="code">tcselection</code> (with the string <code class="code">"external"</code> as value).</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Size( fpgrp : hard, tcselection := "external" );</span>
</pre></div>
<p>Options supplied with function calls in this way are passed down using the global options stack described in chapter <a href="chap8.html#X7FD84061873F72A2"><span class="RefLink">8</span></a>, so that the call above is exactly equivalent to</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">PushOptions( rec( hard := true, tcselection := "external") );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Size( fpgrp );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">PopOptions( );</span>
</pre></div>
<p><em>Note</em> that any option may be passed with any function, whether or not it has any actual meaning for that function, or any function called by it. The system provides no safeguard against misspelled option names.</p>
<p><a id="X7A274A1F8553B7E6" name="X7A274A1F8553B7E6"></a></p>
<h4>4.12 <span class="Heading">Comparisons</span></h4>
<p><code class="code"><var class="Arg">left-expr</var> = <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> <> <var class="Arg">right-expr</var></code></p>
<p>The operator <code class="code">=</code> tests for equality of its two operands and evaluates to <code class="keyw">true</code> if they are equal and to <code class="keyw">false</code> otherwise. Likewise <code class="code"><></code> tests for inequality of its two operands. For each type of objects the definition of equality is given in the respective chapter. Objects in different families (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>) are never equal, i.e., <code class="code">=</code> evaluates in this case to <code class="keyw">false</code>, and <code class="code"><></code> evaluates to <code class="keyw">true</code>.</p>
<p><code class="code"><var class="Arg">left-expr</var> < <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> > <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> <= <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> >= <var class="Arg">right-expr</var></code></p>
<p><code class="code"><</code> denotes less than, <code class="code"><=</code> less than or equal, <code class="code">></code> greater than, and <code class="code">>=</code> greater than or equal of its two operands. For each kind of objects the definition of the ordering is given in the respective chapter.</p>
<p>Note that <code class="code"><</code> implements a <em>total ordering</em> of objects (which can be used for example to sort a list of elements). Therefore in general <code class="code"><</code> will not be compatible with any inclusion relation (which can be tested using <code class="func">IsSubset</code> (<a href="chap30.html#X79CA175481F8105F"><span class="RefLink">30.5-1</span></a>)). (For example, it is possible to compare permutation groups with <code class="code"><</code> in a total ordering of all permutation groups, but this ordering is not compatible with the relation of being a subgroup.)</p>
<p>Only for the following kinds of objects, an ordering via <code class="code"><</code> of objects in <em>different</em> families (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>) is supported. Rationals (see <code class="func">IsRat</code> (<a href="chap17.html#X7ED018F5794935F7"><span class="RefLink">17.2-1</span></a>)) are smallest, next are cyclotomics (see <code class="func">IsCyclotomic</code> (<a href="chap18.html#X841C425281A6F775"><span class="RefLink">18.1-3</span></a>)), followed by finite field elements (see <code class="func">IsFFE</code> (<a href="chap59.html#X7D3DF32C84FEBD25"><span class="RefLink">59.1-1</span></a>)); finite field elements in different characteristics are compared via their characteristics, next are permutations (see <code class="func">IsPerm</code> (<a href="chap42.html#X7AA69C6686FC49EA"><span class="RefLink">42.1-1</span></a>)), followed by the boolean values <code class="keyw">true</code>, <code class="keyw">false</code>, and <code class="keyw">fail</code> (see <code class="func">IsBool</code> (<a href="chap20.html#X7D58580284CF7894"><span class="RefLink">20.1-1</span></a>)), characters (such as <code class="code">{</code>}a{'}', see <code class="func">IsChar</code> (<a href="chap27.html#X80CFAE128560E064"><span class="RefLink">27.1-1</span></a>)), and lists (see <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>)) are largest; note that two lists can be compared with <code class="code"><</code> if and only if their elements are again objects that can be compared with <code class="code"><</code>.</p>
<p>For other objects, <strong class="pkg">GAP</strong> does <em>not</em> provide an ordering via <code class="code"><</code>. The reason for this is that a total ordering of all <strong class="pkg">GAP</strong> objects would be hard to maintain when new kinds of objects are introduced, and such a total ordering is hardly used in its full generality.</p>
<p>However, for objects in the filters listed above, the ordering via <code class="code"><</code> has turned out to be useful. For example, one can form <em>sorted lists</em> containing integers and nested lists of integers, and then search in them using <code class="code">PositionSorted</code> (see <a href="chap21.html#X8196FD4779BCCA0C"><span class="RefLink">21.16</span></a>).</p>
<p>Of course it would in principle be possible to define an ordering via <code class="code"><</code> also for certain other objects, by installing appropriate methods for the operation <code class="code">\<</code>. But this may lead to problems at least as soon as one loads <strong class="pkg">GAP</strong> code in which the same is done, under the assumption that one is completely free to define an ordering via <code class="code"><</code> for other objects than the ones for which the "official" <strong class="pkg">GAP</strong> provides already an ordering via <code class="code"><</code>.</p>
<p>Comparison operators, including the operator <code class="keyw">in</code> (see <a href="chap21.html#X808A207182B2F84F"><span class="RefLink">21.8</span></a>), are not associative, Hence it is not allowed to write <code class="code"><var class="Arg">a</var> = <var class="Arg">b</var> <> <var class="Arg">c</var> = <var class="Arg">d</var></code>, you must use <code class="code">(<var class="Arg">a</var> = <var class="Arg">b</var>) <> (<var class="Arg">c</var> = <var class="Arg">d</var>)</code> instead. The comparison operators have higher precedence than the logical operators (see <a href="chap20.html#X79AD41A185FD7213"><span class="RefLink">20.4</span></a>), but lower precedence than the arithmetic operators (see <a href="chap4.html#X7B66C8707B5DE10A"><span class="RefLink">4.13</span></a>). Thus, for instance, <code class="code"><var class="Arg">a</var> * <var class="Arg">b</var> = <var class="Arg">c</var> and <var class="Arg">d</var></code> is interpreted as <code class="code">((<var class="Arg">a</var> * <var class="Arg">b</var>) = <var class="Arg">c</var>) and <var class="Arg">d</var>)</code>.</p>
<p>The following example shows a comparison where the left operand is an expression.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2 + 9 = Fibonacci(7);</span>
true
</pre></div>
<p>For the underlying operations of the operators introduced above, see <a href="chap31.html#X7B3BC7BA7BB2646D"><span class="RefLink">31.11</span></a>.</p>
<p><a id="X7B66C8707B5DE10A" name="X7B66C8707B5DE10A"></a></p>
<h4>4.13 <span class="Heading">Arithmetic Operators</span></h4>
<p><code class="code">+ <var class="Arg">right-expr</var></code></p>
<p><code class="code">- <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> + <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> - <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> * <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> / <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> mod <var class="Arg">right-expr</var></code></p>
<p><code class="code"><var class="Arg">left-expr</var> ^ <var class="Arg">right-expr</var></code></p>
<p>The arithmetic operators are <code class="code">+</code>, <code class="code">-</code>, <code class="code">*</code>, <code class="code">/</code>, <code class="keyw">mod</code>, and <code class="code">^</code>. The meanings (semantics) of those operators generally depend on the types of the operands involved, and they are defined in the various chapters describing the types. However basically the meanings are as follows.</p>
<p><code class="code"><var class="Arg">a</var> + <var class="Arg">b</var></code> denotes the addition of additive elements <var class="Arg">a</var> and <var class="Arg">b</var>.</p>
<p><code class="code"><var class="Arg">a</var> - <var class="Arg">b</var></code> denotes the addition of <var class="Arg">a</var> and the additive inverse of <var class="Arg">b</var>.</p>
<p><code class="code"><var class="Arg">a</var> * <var class="Arg">b</var></code> denotes the multiplication of multiplicative elements <var class="Arg">a</var> and <var class="Arg">b</var>.</p>
<p><code class="code"><var class="Arg">a</var> / <var class="Arg">b</var></code> denotes the multiplication of <var class="Arg">a</var> with the multiplicative inverse of <var class="Arg">b</var>.</p>
<p><code class="code"><var class="Arg">a</var> mod <var class="Arg">b</var></code>, for integer or rational left operand <var class="Arg">a</var> and for non-zero integer right operand <var class="Arg">b</var>, is defined as follows. If <var class="Arg">a</var> and <var class="Arg">b</var> are both integers, <code class="code"><var class="Arg">a</var> mod <var class="Arg">b</var></code> is the integer <var class="Arg">r</var> in the integer range <code class="code">0 .. |<var class="Arg">b</var>| - 1</code> satisfying <code class="code"><var class="Arg">a</var> = <var class="Arg">r</var> + <var class="Arg">b</var><var class="Arg">q</var></code>, for some integer <var class="Arg">q</var> (where the operations occurring have their usual meaning over the integers, of course).</p>
<p>If <var class="Arg">a</var> is a rational number and <var class="Arg">b</var> is a non-zero integer, and <code class="code"><var class="Arg">a</var> = <var class="Arg">m</var> / <var class="Arg">n</var></code> where <var class="Arg">m</var> and <var class="Arg">n</var> are coprime integers with <var class="Arg">n</var> positive, then <code class="code"><var class="Arg">a</var> mod <var class="Arg">b</var></code> is the integer <var class="Arg">r</var> in the integer range <code class="code">0 .. |<var class="Arg">b</var>| - 1</code> such that <var class="Arg">m</var> is congruent to <code class="code"><var class="Arg">r</var><var class="Arg">n</var></code> modulo <var class="Arg">b</var>, and <var class="Arg">r</var> is called the "modular remainder" of <var class="Arg">a</var> modulo <var class="Arg">b</var>. Also, <code class="code">1 / <var class="Arg">n</var> mod <var class="Arg">b</var></code> is called the "modular inverse" of <var class="Arg">n</var> modulo <var class="Arg">b</var>. (A pair of integers is said to be <em>coprime</em> (or <em>relatively prime</em>) if their greatest common divisor is 1.)</p>
<p>With the above definition, <code class="code">4 / 6 mod 32</code> equals <code class="code">2 / 3 mod 32</code> and hence exists (and is equal to 22), despite the fact that 6 has no inverse modulo 32.</p>
<p><em>Note:</em> For rational <var class="Arg">a</var>, <code class="code"><var class="Arg">a</var> mod <var class="Arg">b</var></code> could have been defined to be the non-negative rational <var class="Arg">c</var> less than <code class="code">|<var class="Arg">b</var>|</code> such that <code class="code"><var class="Arg">a</var> - <var class="Arg">c</var></code> is a multiple of <var class="Arg">b</var>. However this definition is seldom useful and <em>not</em> the one chosen for <strong class="pkg">GAP</strong>.</p>
<p><code class="code">+</code> and <code class="code">-</code> can also be used as unary operations. The unary <code class="code">+</code> is ignored. The unary <code class="code">-</code> returns the additive inverse of its operand; over the integers it is equivalent to multiplication by <code class="code">-1</code>.</p>
<p><code class="code">^</code> denotes powering of a multiplicative element if the right operand is an integer, and is also used to denote the action of a group element on a point of a set if the right operand is a group element.</p>
<p>The <em>precedence</em> of those operators is as follows. The powering operator <code class="code">^</code> has the highest precedence, followed by the unary operators <code class="code">+</code> and <code class="code">-</code>, which are followed by the multiplicative operators <code class="code">*</code>, <code class="code">/</code>, and <code class="keyw">mod</code>, and the additive binary operators <code class="code">+</code> and <code class="code">-</code> have the lowest precedence. That means that the expression <code class="code">-2 ^ -2 * 3 + 1</code> is interpreted as <code class="code">(-(2 ^ (-2)) * 3) + 1</code>. If in doubt use parentheses to clarify your intention.</p>
<p>The <em>associativity</em> of the arithmetic operators is as follows. <code class="code">^</code> is not associative, i.e., it is invalid to write <code class="code">2^3^4</code>, use parentheses to clarify whether you mean <code class="code">(2^3)^4</code> or <code class="code">2^(3^4)</code>. The unary operators <code class="code">+</code> and <code class="code">-</code> are right associative, because they are written to the left of their operands. <code class="code">*</code>, <code class="code">/</code>, <code class="keyw">mod</code>, <code class="code">+</code>, and <code class="code">-</code> are all left associative, i.e., <code class="code">1-2-3</code> is interpreted as <code class="code">(1-2)-3</code> not as <code class="code">1-(2-3)</code>. Again, if in doubt use parentheses to clarify your intentions.</p>
<p>The arithmetic operators have higher precedence than the comparison operators (see <a href="chap4.html#X7A274A1F8553B7E6"><span class="RefLink">4.12</span></a> and <a href="chap30.html#X82D39CF980FDBFFA"><span class="RefLink">30.6</span></a>) and the logical operators (see <a href="chap20.html#X79AD41A185FD7213"><span class="RefLink">20.4</span></a>). Thus, for example, <code class="code"><var class="Arg">a</var> * <var class="Arg">b</var> = <var class="Arg">c</var> and <var class="Arg">d</var></code> is interpreted, <code class="code">((<var class="Arg">a</var> * <var class="Arg">b</var>) = <var class="Arg">c</var>) and <var class="Arg">d</var></code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2 + 9; # a very simple arithmetic expression</span>
13
</pre></div>
<p>For other arithmetic operations, and for the underlying operations of the operators introduced above, see <a href="chap31.html#X7A2914307963E370"><span class="RefLink">31.12</span></a>.</p>
<p><a id="X8543285D87361BE6" name="X8543285D87361BE6"></a></p>
<h4>4.14 <span class="Heading">Statements</span></h4>
<p>Assignments (see <a href="chap4.html#X7E6A50307F4D3FAE"><span class="RefLink">4.15</span></a>), Procedure calls (see <a href="chap4.html#X825803DE78251DA6"><span class="RefLink">4.16</span></a>), <code class="keyw">if</code> statements (see <a href="chap4.html#X875000188622700D"><span class="RefLink">4.17</span></a>), <code class="keyw">while</code> (see <a href="chap4.html#X87AA46408783383F"><span class="RefLink">4.18</span></a>), <code class="keyw">repeat</code> (see <a href="chap4.html#X8295CBF47FAA05C9"><span class="RefLink">4.19</span></a>) and <code class="keyw">for</code> loops (see <a href="chap4.html#X78783E777867638A"><span class="RefLink">4.20</span></a>), and the <code class="keyw">return</code> statement (see <a href="chap4.html#X812C6ABC7A182E9E"><span class="RefLink">4.24</span></a>) are called <em>statements</em>. They can be entered interactively or be part of a function definition. Every statement must be terminated by a semicolon.</p>
<p>Statements, unlike expressions, have no value. They are executed only to produce an effect. For example an assignment has the effect of assigning a value to a variable, a <code class="keyw">for</code> loop has the effect of executing a statement sequence for all elements in a list and so on. We will talk about <em>evaluation</em> of expressions but about <em>execution</em> of statements to emphasize this difference.</p>
<p>Using expressions as statements is treated as syntax error.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 7;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">if i <> 0 then k = 16/i; fi;</span>
Syntax error: := expected
if i <> 0 then k = 16/i; fi;
^
<span class="GAPprompt">gap></span> <span class="GAPinput"></span>
</pre></div>
<p>As you can see from the example this warning does in particular address those users who are used to languages where <code class="code">=</code> instead of <code class="code">:=</code> denotes assignment.</p>
<p>Empty statements are permitted and have no effect.</p>
<p>A sequence of one or more statements is a <em>statement sequence</em>, and may occur everywhere instead of a single statement. Each construct is terminated by a keyword. The simplest statement sequence is a single semicolon, which can be used as an empty statement sequence. In fact an empty statement sequence as in <code class="code">for i in [ 1 .. 2 ] do od</code> is also permitted and is silently translated into the sequence containing just a semicolon.</p>
<p><a id="X7E6A50307F4D3FAE" name="X7E6A50307F4D3FAE"></a></p>
<h4>4.15 <span class="Heading">Assignments</span></h4>
<p><code class="code"><var class="Arg">var</var> := <var class="Arg">expr</var>;</code></p>
<p>The <em>assignment</em> has the effect of assigning the value of the expressions <var class="Arg">expr</var> to the variable <var class="Arg">var</var>.</p>
<p>The variable <var class="Arg">var</var> may be an ordinary variable (see <a href="chap4.html#X7A4C2D0E7E286B4F"><span class="RefLink">4.8</span></a>), a list element selection <code class="code"><var class="Arg">list-var</var>[<var class="Arg">int-expr</var>]</code> (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>) or a record component selection <code class="code"><var class="Arg">record-var</var>.<var class="Arg">ident</var></code> (see <a href="chap29.html#X806DE3BD78742CA4"><span class="RefLink">29.3</span></a>). Since a list element or a record component may itself be a list or a record the left hand side of an assignment may be arbitrarily complex.</p>
<p>Note that variables do not have a type. Thus any value may be assigned to any variable. For example a variable with an integer value may be assigned a permutation or a list or anything else.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">data:= rec( numbers:= [ 1, 2, 3 ] );</span>
rec( numbers := [ 1, 2, 3 ] )
<span class="GAPprompt">gap></span> <span class="GAPinput">data.string:= "string";; data;</span>
rec( numbers := [ 1, 2, 3 ], string := "string" )
<span class="GAPprompt">gap></span> <span class="GAPinput">data.numbers[2]:= 4;; data;</span>
rec( numbers := [ 1, 4, 3 ], string := "string" )
</pre></div>
<p>If the expression <var class="Arg">expr</var> is a function call then this function must return a value. If the function does not return a value an error is signalled and you enter a break loop (see <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>). As usual you can leave the break loop with <code class="code">quit;</code>. If you enter <code class="code">return <var class="Arg">return-expr</var>;</code> the value of the expression <var class="Arg">return-expr</var> is assigned to the variable, and execution continues after the assignment.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">f1:= function( x ) Print( "value: ", x, "\n" ); end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">f2:= function( x ) return f1( x ); end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">f2( 4 );</span>
value: 4
Function Calls: <func> must return a value at
return f1( x );
called from
<function>( <arguments> ) called from read-eval-loop
Entering break read-eval-print loop ...
you can 'quit;' to quit to outer loop, or
you can supply one by 'return <value>;' to continue
<span class="GAPbrkprompt">brk></span> <span class="GAPinput">return "hello";</span>
"hello"
</pre></div>
<p>In the above example, the function <code class="code">f2</code> calls <code class="code">f1</code> with argument <code class="code">4</code>, and since <code class="code">f1</code> does not return a value (but only prints a line "<code class="code">value: ...</code>"), the <code class="keyw">return</code> statement of <code class="code">f2</code> cannot be executed. The error message says that it is possible to return an appropriate value, and the returned string <code class="code">"hello"</code> is used by <code class="code">f2</code> instead of the missing return value of <code class="code">f1</code>.</p>
<p><a id="X825803DE78251DA6" name="X825803DE78251DA6"></a></p>
<h4>4.16 <span class="Heading">Procedure Calls</span></h4>
<p><code class="code"><var class="Arg">procedure-var</var>( [<var class="Arg">arg-expr</var> [,<var class="Arg">arg-expr</var>, ...]] );</code></p>
<p>The <em>procedure call</em> has the effect of calling the procedure <var class="Arg">procedure-var</var>. A procedure call is done exactly like a function call (see <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>). The distinction between functions and procedures is only for the sake of the discussion, <strong class="pkg">GAP</strong> does not distinguish between them. So we state the following conventions.</p>
<p>A <em>function</em> does return a value but does not produce a side effect. As a convention the name of a function is a noun, denoting what the function returns, e.g., <code class="code">"Length"</code>, <code class="code">"Concatenation"</code> and <code class="code">"Order"</code>.</p>
<p>A <em>procedure</em> is a function that does not return a value but produces some effect. Procedures are called only for this effect. As a convention the name of a procedure is a verb, denoting what the procedure does, e.g., <code class="code">"Print"</code>, <code class="code">"Append"</code> and <code class="code">"Sort"</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Read( "myfile.g" ); # a call to the procedure Read</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">l := [ 1, 2 ];;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Append( l, [3,4,5] ); # a call to the procedure Append</span>
</pre></div>
<p>There are a few exceptions of <strong class="pkg">GAP</strong> functions that do both return a value and produce some effect. An example is <code class="func">Sortex</code> (<a href="chap21.html#X87287FCA81E2B06A"><span class="RefLink">21.18-3</span></a>) which sorts a list and returns the corresponding permutation of the entries.</p>
<p><a id="X875000188622700D" name="X875000188622700D"></a></p>
<h4>4.17 <span class="Heading">If</span></h4>
<p><code class="code">if <var class="Arg">bool-expr1</var> then <var class="Arg">statements1</var> { elif <var class="Arg">bool-expr2</var> then <var class="Arg">statements2</var> }[ else <var class="Arg">statements3</var> ] fi;</code></p>
<p>The <code class="keyw">if</code> statement allows one to execute statements depending on the value of some boolean expression. The execution is done as follows.</p>
<p>First the expression <var class="Arg">bool-expr1</var> following the <code class="keyw">if</code> is evaluated. If it evaluates to <code class="keyw">true</code> the statement sequence <var class="Arg">statements1</var> after the first <code class="keyw">then</code> is executed, and the execution of the <code class="keyw">if</code> statement is complete.</p>
<p>Otherwise the expressions <var class="Arg">bool-expr2</var> following the <code class="keyw">elif</code> are evaluated in turn. There may be any number of <code class="keyw">elif</code> parts, possibly none at all. As soon as an expression evaluates to <code class="keyw">true</code> the corresponding statement sequence <var class="Arg">statements2</var> is executed and execution of the <code class="keyw">if</code> statement is complete.</p>
<p>If the <code class="keyw">if</code> expression and all, if any, <code class="keyw">elif</code> expressions evaluate to <code class="keyw">false</code> and there is an <code class="keyw">else</code> part, which is optional, its statement sequence <var class="Arg">statements3</var> is executed and the execution of the <code class="keyw">if</code> statement is complete. If there is no <code class="keyw">else</code> part the <code class="keyw">if</code> statement is complete without executing any statement sequence.</p>
<p>Since the <code class="keyw">if</code> statement is terminated by the <code class="keyw">fi</code> keyword there is no question where an <code class="keyw">else</code> part belongs, i.e., <strong class="pkg">GAP</strong> has no "dangling else". In</p>
<div class="example"><pre>
if expr1 then if expr2 then stats1 else stats2 fi; fi;
</pre></div>
<p>the <code class="keyw">else</code> part belongs to the second <code class="keyw">if</code> statement, whereas in</p>
<div class="example"><pre>
if expr1 then if expr2 then stats1 fi; else stats2 fi;
</pre></div>
<p>the <code class="keyw">else</code> part belongs to the first <code class="keyw">if</code> statement.</p>
<p>Since an <code class="keyw">if</code> statement is not an expression it is not possible to write</p>
<div class="example"><pre>
abs := if x > 0 then x; else -x; fi;
</pre></div>
<p>which would, even if legal syntax, be meaningless, since the <code class="keyw">if</code> statement does not produce a value that could be assigned to <code class="code">abs</code>.</p>
<p>If one of the expressions <var class="Arg">bool-expr1</var>, <var class="Arg">bool-expr2</var> is evaluated and its value is neither <code class="keyw">true</code> nor <code class="keyw">false</code> an error is signalled and a break loop (see <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>) is entered. As usual you can leave the break loop with <code class="code">quit;</code>. If you enter <code class="code">return true;</code>, execution of the <code class="keyw">if</code> statement continues as if the expression whose evaluation failed had evaluated to <code class="keyw">true</code>. Likewise, if you enter <code class="code">return false;</code>, execution of the <code class="keyw">if</code> statement continues as if the expression whose evaluation failed had evaluated to <code class="keyw">false</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 10;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">if 0 < i then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> s := 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> elif i < 0 then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> s := -1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<span class="GAPprompt">></span> <span class="GAPinput"> s := 0;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">s; # the sign of i</span>
1
</pre></div>
<p><a id="X87AA46408783383F" name="X87AA46408783383F"></a></p>
<h4>4.18 <span class="Heading">While</span></h4>
<p><code class="code">while <var class="Arg">bool-expr</var> do <var class="Arg">statements</var> od;</code></p>
<p>The <code class="keyw">while</code> loop executes the statement sequence <var class="Arg">statements</var> while the condition <var class="Arg">bool-expr</var> evaluates to <code class="keyw">true</code>.</p>
<p>First <var class="Arg">bool-expr</var> is evaluated. If it evaluates to <code class="keyw">false</code> execution of the <code class="keyw">while</code> loop terminates and the statement immediately following the <code class="keyw">while</code> loop is executed next. Otherwise if it evaluates to <code class="keyw">true</code> the <var class="Arg">statements</var> are executed and the whole process begins again.</p>
<p>The difference between the <code class="keyw">while</code> loop and the <code class="keyw">repeat</code> <code class="keyw">until</code> loop (see <a href="chap4.html#X8295CBF47FAA05C9"><span class="RefLink">4.19</span></a>) is that the <var class="Arg">statements</var> in the <code class="keyw">repeat</code> <code class="keyw">until</code> loop are executed at least once, while the <var class="Arg">statements</var> in the <code class="keyw">while</code> loop are not executed at all if <var class="Arg">bool-expr</var> is <code class="keyw">false</code> at the first iteration.</p>
<p>If <var class="Arg">bool-expr</var> does not evaluate to <code class="keyw">true</code> or <code class="keyw">false</code> an error is signalled and a break loop (see <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>) is entered. As usual you can leave the break loop with <code class="code">quit;</code>. If you enter <code class="code">return false;</code>, execution continues with the next statement immediately following the <code class="keyw">while</code> loop. If you enter <code class="code">return true;</code>, execution continues at <var class="Arg">statements</var>, after which the next evaluation of <var class="Arg">bool-expr</var> may cause another error.</p>
<p>The following example shows a <code class="keyw">while</code> loop that sums up the squares <span class="SimpleMath">1^2, 2^2, ...</span> until the sum exceeds <span class="SimpleMath">200</span>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 0;; s := 0;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">while s <= 200 do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> i := i + 1; s := s + i^2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> od;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">s;</span>
204
</pre></div>
<p>A <code class="keyw">while</code> loop may be left prematurely using <code class="keyw">break</code>, see <a href="chap4.html#X7B60C6127E183021"><span class="RefLink">4.21</span></a>.</p>
<p><a id="X8295CBF47FAA05C9" name="X8295CBF47FAA05C9"></a></p>
<h4>4.19 <span class="Heading">Repeat</span></h4>
<p><code class="code">repeat <var class="Arg">statements</var> until <var class="Arg">bool-expr</var>;</code></p>
<p>The <code class="keyw">repeat</code> loop executes the statement sequence <var class="Arg">statements</var> until the condition <var class="Arg">bool-expr</var> evaluates to <code class="keyw">true</code>.</p>
<p>First <var class="Arg">statements</var> are executed. Then <var class="Arg">bool-expr</var> is evaluated. If it evaluates to <code class="keyw">true</code> the <code class="keyw">repeat</code> loop terminates and the statement immediately following the <code class="keyw">repeat</code> loop is executed next. Otherwise if it evaluates to <code class="keyw">false</code> the whole process begins again with the execution of the <var class="Arg">statements</var>.</p>
<p>The difference between the <code class="keyw">while</code> loop (see <a href="chap4.html#X87AA46408783383F"><span class="RefLink">4.18</span></a>) and the <code class="keyw">repeat</code> <code class="keyw">until</code> loop is that the <var class="Arg">statements</var> in the <code class="keyw">repeat</code> <code class="keyw">until</code> loop are executed at least once, while the <var class="Arg">statements</var> in the <code class="keyw">while</code> loop are not executed at all if <var class="Arg">bool-expr</var> is <code class="keyw">false</code> at the first iteration.</p>
<p>If <var class="Arg">bool-expr</var> does not evaluate to <code class="keyw">true</code> or <code class="keyw">false</code> an error is signalled and a break loop (see <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>) is entered. As usual you can leave the break loop with <code class="code">quit;</code>. If you enter <code class="code">return true;</code>, execution continues with the next statement immediately following the <code class="keyw">repeat</code> loop. If you enter <code class="code">return false;</code>, execution continues at <var class="Arg">statements</var>, after which the next evaluation of <var class="Arg">bool-expr</var> may cause another error.</p>
<p>The <code class="keyw">repeat</code> loop in the following example has the same purpose as the <code class="keyw">while</code> loop in the preceding example, namely to sum up the squares <span class="SimpleMath">1^2, 2^2, ...</span> until the sum exceeds <span class="SimpleMath">200</span>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 0;; s := 0;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">repeat</span>
<span class="GAPprompt">></span> <span class="GAPinput"> i := i + 1; s := s + i^2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> until s > 200;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">s;</span>
204
</pre></div>
<p>A <code class="keyw">repeat</code> loop may be left prematurely using <code class="keyw">break</code>, see <a href="chap4.html#X7B60C6127E183021"><span class="RefLink">4.21</span></a>.</p>
<p><a id="X78783E777867638A" name="X78783E777867638A"></a></p>
<h4>4.20 <span class="Heading">For</span></h4>
<p><code class="code">for <var class="Arg">simple-var</var> in <var class="Arg">list-expr</var> do <var class="Arg">statements</var> od;</code></p>
<p>The <code class="keyw">for</code> loop executes the statement sequence <var class="Arg">statements</var> for every element of the list <var class="Arg">list-expr</var>.</p>
<p>The statement sequence <var class="Arg">statements</var> is first executed with <var class="Arg">simple-var</var> bound to the first element of the list <var class="Arg">list-expr</var>, then with <var class="Arg">simple-var</var> bound to the second element of <var class="Arg">list-expr</var> and so on. <var class="Arg">simple-var</var> must be a simple variable, it must not be a list element selection <code class="code"><var class="Arg">list-var</var>[<var class="Arg">int-expr</var>]</code> or a record component selection <code class="code"><var class="Arg">record-var</var>.<var class="Arg">ident</var></code>.</p>
<p>The execution of the <code class="keyw">for</code> loop over a list is exactly equivalent to the following <code class="keyw">while</code> loop.</p>
<div class="example"><pre>
loop_list := list;
loop_index := 1;
while loop_index <= Length(loop_list) do
variable := loop_list[loop_index];
statements
loop_index := loop_index + 1;
od;
</pre></div>
<p>with the exception that "loop_list" and "loop_index" are different variables for each <code class="keyw">for</code> loop, i.e., these variables of different <code class="keyw">for</code> loops do not interfere with each other.</p>
<p>The list <var class="Arg">list-expr</var> is very often a range (see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a>).</p>
<p><code class="code">for <var class="Arg">variable</var> in [<var class="Arg">from</var>..<var class="Arg">to</var>] do <var class="Arg">statements</var> od;</code></p>
<p>corresponds to the more common</p>
<p><code class="code">for <var class="Arg">variable</var> from <var class="Arg">from</var> to <var class="Arg">to</var> do <var class="Arg">statements</var> od;</code></p>
<p>in other programming languages.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">s := 0;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">for i in [1..100] do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> s := s + i;</span>
<span class="GAPprompt">></span> <span class="GAPinput">od;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">s;</span>
5050
</pre></div>
<p>Note in the following example how the modification of the <em>list</em> in the loop body causes the loop body also to be executed for the new values.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">l := [ 1, 2, 3, 4, 5, 6 ];;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">for i in l do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Print( i, " " );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if i mod 2 = 0 then Add( l, 3 * i / 2 ); fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput">od; Print( "\n" );</span>
1 2 3 4 5 6 3 6 9 9
<span class="GAPprompt">gap></span> <span class="GAPinput">l;</span>
[ 1, 2, 3, 4, 5, 6, 3, 6, 9, 9 ]
</pre></div>
<p>Note in the following example that the modification of the <em>variable</em> that holds the list has no influence on the loop.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">l := [ 1, 2, 3, 4, 5, 6 ];;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">for i in l do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Print( i, " " );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> l := [];</span>
<span class="GAPprompt">></span> <span class="GAPinput">od; Print( "\n" );</span>
1 2 3 4 5 6
<span class="GAPprompt">gap></span> <span class="GAPinput">l;</span>
[ ]
</pre></div>
<p><code class="code">for <var class="Arg">variable</var> in <var class="Arg">iterator</var> do <var class="Arg">statements</var> od;</code></p>
<p>It is also possible to have a <code class="keyw">for</code>-loop run over an iterator (see <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a>). In this case the <code class="keyw">for</code>-loop is equivalent to</p>
<div class="example"><pre>
while not IsDoneIterator(iterator) do
variable := NextIterator(iterator)
statements
od;
</pre></div>
<p><code class="code">for <var class="Arg">variable</var> in <var class="Arg">object</var> do <var class="Arg">statements</var> od;</code></p>
<p>Finally, if an object <var class="Arg">object</var> which is not a list or an iterator appears in a <code class="keyw">for</code>-loop, then <strong class="pkg">GAP</strong> will attempt to evaluate the function call <code class="code">Iterator(<var class="Arg">object</var>)</code>. If this is successful then the loop is taken to run over the iterator returned.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">g := Group((1,2,3,4,5),(1,2)(3,4)(5,6));</span>
Group([ (1,2,3,4,5), (1,2)(3,4)(5,6) ])
<span class="GAPprompt">gap></span> <span class="GAPinput">count := 0;; sumord := 0;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">for x in g do</span>
<span class="GAPprompt">></span> <span class="GAPinput">count := count + 1; sumord := sumord + Order(x); od;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">count;</span>
120
<span class="GAPprompt">gap></span> <span class="GAPinput">sumord;</span>
471
</pre></div>
<p>The effect of</p>
<p><code class="code">for <var class="Arg">variable</var> in <var class="Arg">domain</var> do</code></p>
<p>should thus normally be the same as</p>
<p><code class="code">for <var class="Arg">variable</var> in AsList(<var class="Arg">domain</var>) do</code></p>
<p>but may use much less storage, as the iterator may be more compact than a list of all the elements.</p>
<p>See <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a> for details about iterators.</p>
<p>A <code class="keyw">for</code> loop may be left prematurely using <code class="keyw">break</code>, see <a href="chap4.html#X7B60C6127E183021"><span class="RefLink">4.21</span></a>. This combines especially well with a loop over an iterator, as a way of searching through a domain for an element with some useful property.</p>
<p><a id="X7B60C6127E183021" name="X7B60C6127E183021"></a></p>
<h4>4.21 <span class="Heading">Break</span></h4>
<p><code class="code">break;</code></p>
<p>The statement <code class="code">break;</code> causes an immediate exit from the innermost loop enclosing it.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">g := Group((1,2,3,4,5),(1,2)(3,4)(5,6));</span>
Group([ (1,2,3,4,5), (1,2)(3,4)(5,6) ])
<span class="GAPprompt">gap></span> <span class="GAPinput">for x in g do</span>
<span class="GAPprompt">></span> <span class="GAPinput">if Order(x) = 3 then</span>
<span class="GAPprompt">></span> <span class="GAPinput">break;</span>
<span class="GAPprompt">></span> <span class="GAPinput">fi; od;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">x;</span>
(1,4,3)(2,6,5)
</pre></div>
<p>It is an error to use this statement other than inside a loop.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">break;</span>
Error, A break statement can only appear inside a loop
not in any function
</pre></div>
<p><a id="X7CCBA2247AA366BD" name="X7CCBA2247AA366BD"></a></p>
<h4>4.22 <span class="Heading">Continue</span></h4>
<p><code class="code">continue;</code></p>
<p>The statement <code class="code">continue;</code> causes the rest of the current iteration of the innermost loop enclosing it to be skipped.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">g := Group((1,2,3),(1,2));</span>
Group([ (1,2,3), (1,2) ])
<span class="GAPprompt">gap></span> <span class="GAPinput">for x in g do</span>
<span class="GAPprompt">></span> <span class="GAPinput">if Order(x) = 3 then</span>
<span class="GAPprompt">></span> <span class="GAPinput">continue;</span>
<span class="GAPprompt">></span> <span class="GAPinput">fi; Print(x,"\n"); od;</span>
()
(2,3)
(1,3)
(1,2)
</pre></div>
<p>It is an error to use this statement other than inside a loop.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">continue;</span>
Error, A continue statement can only appear inside a loop
not in any function
</pre></div>
<p><a id="X815F71EA7BC0EB6F" name="X815F71EA7BC0EB6F"></a></p>
<h4>4.23 <span class="Heading">Function</span></h4>
<p><code class="code">function( [ <var class="Arg">arg-ident</var> {, <var class="Arg">arg-ident</var>} ] )</code></p>
<p><code class="code"> [local <var class="Arg">loc-ident</var> {, <var class="Arg">loc-ident</var>} ; ]</code></p>
<p><code class="code"> <var class="Arg">statements</var></code></p>
<p><code class="code">end</code></p>
<p>A function is in fact a literal and not a statement. Such a function literal can be assigned to a variable or to a list element or a record component. Later this function can be called as described in <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>.</p>
<p>The following is an example of a function definition. It is a function to compute values of the Fibonacci sequence (see <code class="func">Fibonacci</code> (<a href="chap16.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>)).</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">fib := function ( n )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local f1, f2, f3, i;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := 1; f2 := 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> for i in [3..n] do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f3 := f1 + f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f2 := f3;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> od;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return f2;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">List( [1..10], fib );</span>
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre></div>
<p>Because for each of the formal arguments <var class="Arg">arg-ident</var> and for each of the formal locals <var class="Arg">loc-ident</var> a new variable is allocated when the function is called (see <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>), it is possible that a function calls itself. This is usually called <em>recursion</em>. The following is a recursive function that computes values of the Fibonacci sequence.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">fib := function ( n )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if n < 3 then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return fib(n-1) + fib(n-2);</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">List( [1..10], fib );</span>
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
</pre></div>
<p>Note that the recursive version needs <code class="code">2 * fib(<var class="Arg">n</var>)-1</code> steps to compute <code class="code">fib(<var class="Arg">n</var>)</code>, while the iterative version of <code class="code">fib</code> needs only <code class="code"><var class="Arg">n</var>-2</code> steps. Both are not optimal however, the library function <code class="func">Fibonacci</code> (<a href="chap16.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>) only needs about <code class="code">Log(<var class="Arg">n</var>)</code> steps.</p>
<p>As noted in Section <a href="chap4.html#X78C70489791FDF43"><span class="RefLink">4.11</span></a>, the case where a function is defined with exactly one formal argument with the name <code class="code">arg</code>, is special. It provides a way of defining a function with a variable number of arguments; the values of all the actual arguments are stored in a list and this list is assigned to the new variable corresponding to the formal argument <code class="code">arg</code>. There are two typical scenarios for wanting such a possibility: having optional arguments and having any number of arguments.</p>
<p>The following example shows one way that the function <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) might be encoded and demonstrates the "optional argument" scenario.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">position := function ( arg ) </span>
<span class="GAPprompt">></span> <span class="GAPinput"> local list, obj, pos;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> list := arg[1];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> obj := arg[2];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if 2 = Length(arg) then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> pos := 0;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<span class="GAPprompt">></span> <span class="GAPinput"> pos := arg[3];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> repeat</span>
<span class="GAPprompt">></span> <span class="GAPinput"> pos := pos + 1;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> if pos > Length(list) then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return fail;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> until list[pos] = obj;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return pos;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;</span>
function( arg ) ... end
<span class="GAPprompt">gap></span> <span class="GAPinput">position([1, 4, 2], 4);</span>
2
<span class="GAPprompt">gap></span> <span class="GAPinput">position([1, 4, 2], 3);</span>
fail
<span class="GAPprompt">gap></span> <span class="GAPinput">position([1, 4, 2], 4, 2);</span>
fail
</pre></div>
<p>The following example demonstrates the "any number of arguments" scenario.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">sum := function ( arg )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local total, x;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> total := 0;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> for x in arg do</span>
<span class="GAPprompt">></span> <span class="GAPinput"> total := total + x;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> od;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return total;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;</span>
function( arg ) ... end
<span class="GAPprompt">gap></span> <span class="GAPinput">sum(1, 2, 3);</span>
6
<span class="GAPprompt">gap></span> <span class="GAPinput">sum(1, 2, 3, 4);</span>
10
<span class="GAPprompt">gap></span> <span class="GAPinput">sum();</span>
0
</pre></div>
<p>The user should compare the above with the <strong class="pkg">GAP</strong> function <code class="func">Sum</code> (<a href="chap21.html#X7A04B71C84CFCC2D"><span class="RefLink">21.20-26</span></a>) which, for example, may take a list argument and optionally an initial element (which zero should the sum of an empty list return?).</p>
<p>Note that if a function <var class="Arg">f</var> is defined as above with the single formal argument <code class="code">arg</code> then <code class="code">NumberArgumentsFunction(<var class="Arg">f</var>)</code> returns <span class="SimpleMath">-1</span> (see <code class="func">NumberArgumentsFunction</code> (<a href="chap5.html#X877F03F77FD74C98"><span class="RefLink">5.1-2</span></a>)).</p>
<p>The argument <code class="code">arg</code> when used as the single argument name of some function <var class="Arg">f</var> tells <strong class="pkg">GAP</strong> that when it encounters <var class="Arg">f</var> that it should form a list out of the arguments of <var class="Arg">f</var>. What if one wishes to do the "opposite": tell <strong class="pkg">GAP</strong> that a list should be "unwrapped" and passed as several arguments to a function. The function <code class="func">CallFuncList</code> (<a href="chap5.html#X7CF4DDB97D65AE52"><span class="RefLink">5.2-1</span></a>) is provided for this purpose.</p>
<p>Also see Chapter <a href="chap5.html#X86FA580F8055B274"><span class="RefLink">5</span></a>.</p>
<p><code class="code"><var class="Arg">arg-ident</var> -> <var class="Arg">expr</var></code></p>
<p>This is a shorthand for</p>
<p><code class="code">function ( <var class="Arg">arg-ident</var> ) return <var class="Arg">expr</var>; end.</code></p>
<p><var class="Arg">arg-ident</var> must be a single identifier, i.e., it is not possible to write functions of several arguments this way. Also <code class="code">arg</code> is not treated specially, so it is also impossible to write functions that take a variable number of arguments this way.</p>
<p>The following is an example of a typical use of such a function</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Sum( List( [1..100], x -> x^2 ) );</span>
338350
</pre></div>
<p>When the definition of a function <var class="Arg">fun1</var> is evaluated inside another function <var class="Arg">fun2</var>, <strong class="pkg">GAP</strong> binds all the identifiers inside the function <var class="Arg">fun1</var> that are identifiers of an argument or a local of <var class="Arg">fun2</var> to the corresponding variable. This set of bindings is called the environment of the function <var class="Arg">fun1</var>. When <var class="Arg">fun1</var> is called, its body is executed in this environment. The following implementation of a simple stack uses this. Values can be pushed onto the stack and then later be popped off again. The interesting thing here is that the functions <code class="code">push</code> and <code class="code">pop</code> in the record returned by <code class="code">Stack</code> access the local variable <code class="code">stack</code> of <code class="code">Stack</code>. When <code class="code">Stack</code> is called, a new variable for the identifier <code class="code">stack</code> is created. When the function definitions of <code class="code">push</code> and <code class="code">pop</code> are then evaluated (as part of the <code class="keyw">return</code> statement) each reference to <code class="code">stack</code> is bound to this new variable. Note also that the two stacks <code class="code">A</code> and <code class="code">B</code> do not interfere, because each call of <code class="code">Stack</code> creates a new variable for <code class="code">stack</code>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">Stack := function ()</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local stack;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> stack := [];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return rec(</span>
<span class="GAPprompt">></span> <span class="GAPinput"> push := function ( value )</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Add( stack, value );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end,</span>
<span class="GAPprompt">></span> <span class="GAPinput"> pop := function ()</span>
<span class="GAPprompt">></span> <span class="GAPinput"> local value;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> value := stack[Length(stack)];</span>
<span class="GAPprompt">></span> <span class="GAPinput"> Unbind( stack[Length(stack)] );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return value;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end</span>
<span class="GAPprompt">></span> <span class="GAPinput"> );</span>
<span class="GAPprompt">></span> <span class="GAPinput"> end;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">A := Stack();;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">B := Stack();;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">A.push( 1 ); A.push( 2 ); A.push( 3 );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">B.push( 4 ); B.push( 5 ); B.push( 6 );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">A.pop(); A.pop(); A.pop();</span>
3
2
1
<span class="GAPprompt">gap></span> <span class="GAPinput">B.pop(); B.pop(); B.pop();</span>
6
5
4
</pre></div>
<p>This feature should be used rarely, since its implementation in <strong class="pkg">GAP</strong> is not very efficient.</p>
<p><a id="X812C6ABC7A182E9E" name="X812C6ABC7A182E9E"></a></p>
<h4>4.24 <span class="Heading">Return (With or without Value)</span></h4>
<p><code class="code">return;</code></p>
<p>In this form <code class="keyw">return</code> terminates the call of the innermost function that is currently executing, and control returns to the calling function. An error is signalled if no function is currently executing. No value is returned by the function.</p>
<p><code class="code">return <var class="Arg">expr</var>;</code></p>
<p>In this form <code class="keyw">return</code> terminates the call of the innermost function that is currently executing, and returns the value of the expression <var class="Arg">expr</var>. Control returns to the calling function. An error is signalled if no function is currently executing.</p>
<p>Both statements can also be used in break loops (see <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>). <code class="code">return;</code> has the effect that the computation continues where it was interrupted by an error or the user hitting <strong class="button">Ctrl-C</strong>. <code class="code">return <var class="Arg">expr</var>;</code> can be used to continue execution after an error. What happens with the value <var class="Arg">expr</var> depends on the particular error.</p>
<p>For examples of <code class="keyw">return</code> statements, see the functions <code class="code">fib</code> and <code class="code">Stack</code> in Section <a href="chap4.html#X815F71EA7BC0EB6F"><span class="RefLink">4.23</span></a>.</p>
<div class="chlinkprevnextbot"> <a href="chap0.html">[Top of Book]</a> <a href="chap0.html#contents">[Contents]</a> <a href="chap3.html">[Previous Chapter]</a> <a href="chap5.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="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</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>
|