Quelle chap4_mj.html
Sprache: HTML
|
|
| products/Sources/formale Sprachen/GAP/doc/ref/chap4_mj.html |
 |
<? "http:/www.w3.org/TR/html1/DTD/xhtml1-strict.dtd">
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3src="https.jsdelivr//mathjax/MathJaxjs=TeXAMSMML_HTMLorMML>
<< http=-type content/ html charsetUTF- /
< head
< script=text
src= "https.js" type "/javascript>/>
</ script
< title>GAP (ref) <>
< meta class onload)
< meta name= "generator" content=
<rel "" type/ "href"manual " />
< scriptsrc=.js=textjavascript>/>
< script type= "text/javascript">overwriteStyle(
</ headjava.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 7
<bodyclass" ="jscontent)>
<div class="chlinktop"><span/pan>
<div class="chlinkprevnexttop"> <a href="chap0_mj.html">[Top of Book]</a> <a href="chap0_mj.html#contents">[Contents]</a> <a href="chap3_mj.html">[Previous Chapter]</a> <a href="chap5_mj.html">[Next Chapter]</a> </div>
<p id="mathjaxlink" class="pcenter"><a href="chap4.html">[MathJax off]</a></p>
<p>/span>
<divclass"< =chap4_mjhtmlX7FE7C0C17E1ED118">4span="Heading> ProgrammingLanguage/>/>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7B5FF6827DFBDF20>4.1 <span class="Heading>Language<span/>
<span
</div>
<div /span>
<
</div
<div class="ContSect">span="ContSS"><br>span class=nocssnbsp;/><a href.#X85CF993B7D19F2C44.-1IsValidIdentifier</></pan
</span>
</ivspan/div>/>
< class"< =""><span class="nocss" </span><a ="chap4_mj#X7C53CEFC8641B919">.4 spanclass="Heading<spanajava.lang.StringIndexOutOfBoundsException: Index 167 out of bounds for length 167
<span
</div>
<div class="ContSect"><span/pan>
</span>
</div>
<divclass=ContSectspan class=tocline"<span =nocss>nbsp;</pan< href"chap4_mjX860313A179A5163F>46< classHeadingIdentifiers<></>
</span>
<div class="ContSSBlock">
< ="ontSS"><br >span class"  <span>ahref"chap4_mj.htmlX85CF993B7D19F2C4>.- IsValidIdentifier/>/>
<<div/>
</span ="ContSect>spanclass"toclinespan="nocss">&;</span>a href=.htmlX816FBEEA85782EC2"49 <panclass=Heading>oreAboutGlobal </>/>
</div ="ContSSBlock>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7BAFE9C1817253C6">4.7 <span class="Heading"< classContSS />spanclass"nbsp;;</span>ahref=chap4_mj.html#7CD3523B84744EB2">.-1IsReadOnlyGlobal</>/>
java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 7
<java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
<div"">span=tocline ="nocss">&;</spana href".html#X7A4C2D0E7E286B4F>.8< ="Heading">Variables<span<a>
</span>
<div class="ContSSBlock">
<spanspan=""><br/>span=""&;nbsp>ahref.htmlX823D4BC378395B32>.- IsBoundGlobal>span
< class"><br /spanclass""> &;</span<a href="chap4_mjhtml#X7BABB3E77F52626C".-2Unbind/>/pan>
</div></divContSS /< class"&; /><ahref".htmlX7D39D3E17CF49F5B"49- BindGlobal</a</pan>
<divclass"< =tocline>spanclass""&;/><href".#X816FBEEA85782EC2. span="Heading>MoreAbout Global </pan><ajava.lang.StringIndexOutOfBoundsException: Index 183 out of bounds for length 183
</span
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7CD3523B84744EB2">4.9-1 IsReadOnlyGlobalspan="ContSS"><br/< classnocssnbsp;<span href.#X870169447AF490D8.-11NamesUserGVars</>/>
<spanclass"<br />spanclass"">nbspnbsp<span>a ="hap4_mj#X850CE44478254F2792MakeReadOnlyGlobalspan
<span class="ContSS"><br /><span>
span"br>span class=">;&bsp<ahrefhtml".4MakeConstantGlobal<a>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj./>
<span ="ContSS>br/< ="">  </pan><a href="chap4_mj#823">496 IsBoundGlobal</><span
<span class="ContSS"><br /><spanspan
<span class class"">br >span=nocss&; /><a href=chap4_mjhtmlX80B93A9C7E0A57F4.- < classHeading Call Argumentsspan>ajava.lang.StringIndexOutOfBoundsException: Index 176 out of bounds for length 176
<pan="ContSS>br/< class""&;nbsp<span< =chap4_mj.html#X876A6EB68745A510"499 NamesGVars/>/>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X7E604AF579A7BC92">4.9-10 NamesSystemGVars</a></span>
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X870169447AF490D8">4.9-11 NamesUserGVars</a></span>
</div</>
<div class="ContSect"><span class="tocline"><span class="nocss"> </>
</pan>
</div>
<div class="ContSect">span>
</span>
</div>
<iv class"ContSect><span class="tocline"><span class=nocss" </spanahref"hap4_mjhtml#78C70489791FDF43">4.2< ="Heading"Function</>/a>
<>
<div class=ContSSBlock"
<spanclass="ContSS"><br/<span classnocss>nbsp;</spana href="chap4_mj.html#X80B93A9C7E0A57F4">4.2- <span class=HeadingFunction Call Arguments<span>/java.lang.StringIndexOutOfBoundsException: Index 176 out of bounds for length 176
<span
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X867D54987EF86D1D">4.12-2 <span class="Heading">Function Call With Options</span></a>
</span
</div></div>
<div class="ContSect"><span class="tocline"><span class<span>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X7B66C8707B5DE10A">4.14 <span class="Heading">Arithmetic Operators</span></a>
</span>
</div>
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X8543285D87361BE6">4.15 <span class="Heading">Statements</span></a>
<span
<div class="ContSSBlock">
<span class classContSS /< class" nbsp<span>a href=".html#X7B60C6127E183021>.- span=Heading</span/>
</span<pan>
<spanclass"><br/<span class"nocss>nbsp;nbspspan href=chap4_mj.#X825803DE78251DA6.- span="eading>ProcedureCalls<span<a
</span>
<span class="ContSS"><br/span>
<span>
<span class="ContSS"><br />>
</span
<span class="divclass="ContSectspan">span class="">&;</>< ="chap4_mjX8732D9257FFCEA1B.< =Heading Trees</span>
</span =ContSS class"; <><a=chap4_mj"416-SyntaxTreespan
<span/>
</spanh3> span ="Heading">The Language/></h3
<span class>his describesthe class=pkg"GAP/>programming language Itshouldallowyou principle,to predicttheresult ofeachandeveryinput In toknowwhatwearetalkingabout,wefirsthaveto moreclosely attheprocessof interpretation andthe representationsof datainvolved./>
</span>
<span class< id" =X7B5FF6827DFBDF20">/>/>
</span>
<s""><br/>span ="nocss">nbsp;;</span>a href=chap4_mjX812C6ABC7A182E9E4.59 span="Heading>Return( orwithout Value<span<a
</span
<
<div class="ContSect"><span class="tocline"><span class="nocss"> </span><a href="chap4_mj.html#X8732D9257FFCEA1B">4.16 <span class point theinput analyzed and inputs are legalexternal representations, accordingto givenbelow,arerejected errorsThose rules usually the<>syntaxemofaprogramming language./pjava.lang.StringIndexOutOfBoundsException: Index 365 out of bounds for length 365
<span
<div class="ContSSBlock">
<span class="ContSS"><br /><span class="nocss"> </span><a href="chap4_mj.html#X81558D66810BEA67">4.16-1 SyntaxTree</a></span>
<div/>
</div>
<h3
<p>This chapter last is <emprinting<em>.It takes value by evaluation creates, i.. string characters. What do this externalrepresentationis to. can at, paste it with the intoanother window write tofile./>
<p><a id="X7B5FF6827DFBDF20" name="X7B5FF6827DFBDF20"></a></p>
<h4>4.1
<
<p>The input, i< classexample<>
<>The internalrepresentation createdby is either <em now them. exactof internal does matter could be of equal the representation which the would need check errors could a series machine for the processor on which <strong class="pkg">GAP</strong> is running, in which case thereadingwould moreappropriately be .It in a-like.</>
<p>After the input has been/rediv>
<p>The result of the evaluation is, not surprisingly, called a <em>value</em>. Again the form in which such
</\
<p>Lets look at an example
<div class""><prejava.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
1 + 2 * 3;
</pre></div>
<p><strong<Inthisfashionwe can predict result every when knowthe syntactic rulesthat govern the processof reading the rules tell for expression its is in of values thesubexpressions.The rules given sections <ahref="hap4_mj.#X80A85A707B6F4BE7>span class="efLink>4.2</></a, ahref=chap4_mj#X7E90E6607F4E4943><spanclass"">43</></a> <ahref=chap4_mj.#X7C53CEFC8641B919">spanclass="">4./></a>, <<a href="chap4_mjhtmlX87506BDC7D5F789E< =RefLinkspan>anda =chap4_mjspan=.>/,the chap4_mj=.<>/> a=chap4_mj>span"48/>/> < =chap4_mj.#X78C70489791FDF43"> =RefLink>12/pan, href#X7A274A1F8553B7E6.#"><span class="RefLink414/>/,< =chap4_mjhtmlX8543285D87361BE6>span=RefLink415/></>, <a hrefchap4_mjX7E6A50307F4D3FAE< class"">4.15-1</></a>, <a ="chap4_mjhtml#X825803DE78251DA6"><span ="RefLink">15<span>/>,<a ="chap4_mj.html#X875000188622700D> ="RefLink>15<span/> <a href"chap4_mj.html#X87AA46408783383F"><spanclass=RefLink4154/><,ahref.htmlX8295CBF47FAA05C9"<spanclassRefLink4155<span><a>,<a href=hap4_mjhtmlX78783E777867638A"><span="RefLink">4.6/span></>, ahref"chap4_mj."span="411/span</>, the chapters describing the individual data types./>
<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
<>n fashion we canpredict resultof everyinput when we know thesyntacticrules governthe process of reading and the semanticrules thattell usforevery expression itsvalue computed in terms the of the subexpressions. rules in sections< href=chap4_mjhtmlX80A85A707B6F4BE7>span"RefLink".2</></> ahref"chap4_mjhtml#X7E90E6607F4E4943"><spanclassRefLink./span/> ahref.htmlX7C53CEFC8641B919"<span class="RefLink>4.<spana, < href.html#X87506BDC7D5F789E">span class="">4.</span></> and < href="chap4_mj.tmlX860313A179A5163F>span"efLink>.6<span<a,thesemantic rules giveninsections <a href="chap4_mj.htmlX7BAFE9C1817253C6><spanclass="efLink">.7<span>/> ahref.#X7A4C2D0E7E286B4F< classRefLink48<></> < href=chap4_mj.htmlX78C70489791FDF43">spanclass=RefLink>4.12</pan></a>, a href"#">< class="">413/></> ahref"chap4_mj.tml"<span class="RefLink">./pan>a> a ="chap4_mj.htmlX8543285D87361BE6><classRefLink>.<spana> a href"chap4_mj.html#X7E6A50307F4D3FAE">< class=RefLink4.-1<</span/,<=.#X825803DE78251DA6"> "RefLink>.-</span<a>, <ahrefchap4_mj#"<class=RefLink">415-</span>/,< href"chap4_mj.html#X87AA46408783383F< class="RefLink"4.4span>/,ahref="hap4_mj.html"<pan class=>4.155/span><a> ahref"chap4_mjhtmlX78783E777867638A<span="RefLink">4.15-6</></> <a ="chap4_mjhtml#815F71EA7BC0EB6F"><spanclass"./span</, describingtheindividual datatypes<p
<p><a id="X80A85A707B6F4BE7" name="X80A85A707B6F4BE7"></a
<h4>4.2 <span'() + ,
<p>Most. : ; <= gtjava.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
<p>Digits, uppercase and lowercase letters, <strong class="button">Space</strong>, <strong class="button">Tab</strong>, <strong class="button">java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<div class="example"><java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
"' ( ,-#
. / : ; < = >
[ \]^_{ } !
</pre></div>
<p
<
<44. span class=""Symbols>/>
<p>The process of reading, i.e. > &;= [
<p>A <em>keyword</em> is }()
<p>Operator
< class"pre
+ - *
=&t> lt &;gt
:= . ..
] java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
/<>
<p>Note also that during the process of scanning all whitespace is removed
<p><a
ing<span
<p>The i<0thena:-; a:=i;;
<p>A <em>comment<java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p>For example, i < 0 then # if i is negative
<div class="example"><pre>
if i< a : -i take its additive inverse
</pre></div>
<p>is equivalent to</p>
<div a : i;# take itself
if ifi;
></div>
else
a =i take
fi
</re><div
<p(whichby way that is to superfluous). However first is <em>not/>equivalent</p>
<
ifi>since keywordcode class"">if</> must separatedfrom theidentifier code="code"></>by, and<code=keyw<codeand =code"><code>,and<code class="keyw<>andclass"><codemust be separated</>
</pre></div>
<p>since the keyword <code class="keyw">if</code> must be separated
<p><a id="X87506BDC7D5F789E" name="X87506BDC7D5F789E"></a></p>
<h4>4.5 <span class
<p><span <>
<div class="example"><pre class"gapgt</pan spanclass=GAPinput">:= List[0.Int)1] i&; keysi+[1 .4 })<span>
<span span class"">gap;</span>< classGAPinputif mod4 lt;; 0 thenAdd(arr,keys*Int(/)+ ..l ] )fi;<>
<span class="GAPprompt">gap> class"gapgt<span <pan =GAPinput>( ;PrintArray(arr)<spanjava.lang.StringIndexOutOfBoundsException: Index 103 out of bounds for length 103
<span class="GAPprompt">gap TryNextMethod , , ],
<span class="GAPprompt">gap></span> <span class="GAPinput">Length( keys ); PrintArray( arr );</span>
35
[ [ Assert, Info, IsBound, QUIT, end, fi ]
[ TryNextMethod, Unbind, and, atomic ],
[ break, continue, do, elif ],
[ else, end, false/>
for,ifin
[ local, mod, not, od ],
[ or, quit, readonly, readwrite ],
[rec, return then,
[ true, until,
<><div
<p>Note that< id=X860313A179A5163FnameX860313A179A5163Fa>/>
<p<>Note/em: from thelist ofkeywords mayappearto benormal representing or of kinds areactually implemented keywords technical. The consequenceofthis is thatthose identifiers cannotbe re-ssigned, anddo actually function bound them could assigned other or tofunctions keywords are class=keyw</code code="">false<code,code="funcAssert/code>(< =chap7_mj.html#X830E443284780FB9">span="">75-<spana>,<odeclass=func<code < href.htmlX842B89D4860FD5DB>span=RefLink.-<spana) code="func></>< href=chap4_mjhtml#X7BABB3E77F52626C>spanclass"">4.8-</span></a>),<code class="func><code( =chap7_mj.#X864E4B6886E2697Dspan"">46</>/> code=func"TryNextMethod/code (a ="chap78_mj#X7EED949B83046A7F"<span class=RefLink">78.5-<span>/>./>
<p>Keywords <code class="code">atomic</code>, <code class="code">readonly</code>, <code class="code">readwrite</code> are not used at the moment. They are reserved for the future version of GAP to prevent their accidental use as identifiers.</p>
<pjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<h4> Hello
<> <emidentifier/m> refer a variable seehref=chap4_mjhtmlX7A4C2D0E7E286B4F>"".>/a>.An usuallyof digits< ="">_</>,and q><q>characters code="code"@/odeand atonedigitidentifier thecharacter in class thatqat>character class"><code> used to implement namespaces, seeSection< ="chap4_mj#X7DF8774F7D542298> class">4.0</pan<a details./>
<p>Examples of valid identifiers are</p>
<div class="example"><pre>
a foo aLongIdentifier
hello HELLO
x100@def
some_people_prefer_underscores_to_separate_words
WePreferMixedCaseToSeparateWords
abc@def
</pre
<p>Note that case
<The < class=code><code can toother in backslash by equivalentthe except escapeis to letter example>
<div class="example"><pre>
G\2,5\)
</pre
<p>is an identifier, not a call to a function <code class="code">G</code>.<p>isan, not call to a function <code class"code"G<code></>
<p>An
<p>The
< ="85CF993B7D19F2C4="X85CF993B7D19F2C4/java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
<h5>4.6-1 IsValidIdentifier</h5>
<div class="func"><table class="func" width="100%"><tr Arg</var form valid consisting letters andunderscores otherwise it returns codeclass"">false>. Itdoes checkwhether<var="rg>str</var>contains characters by abackslash <code class=code"></code><pjava.lang.StringIndexOutOfBoundsException: Index 342 out of bounds for length 342
<p>returns <code class="keyw">true</code> if the string <var class="Arg">str</var> wouldjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p>Note that the <q>at</q>-character is used java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p><a id="X839A7F8E84BBCA57" name="X839A7F8E84BBCA57"></a></java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<h5>4.6-2 <spanForexample, <strong="">GAP> input assigns user whose start capitalletters mayrun intoerrorswithnewerversion <strong="">GAP> orin GAP>session newer, because it happen these are global in situation/>
<p>The following is also Sectiona href"../doc/tut/chap2_mj.htmlX82F47EA88347AC2D">span="">Tutorial versus</pana.<p>
pThe almost variable < classGAP>library < classGAP startscapitalemSee=html =RefLink>/ for exceptions user recommend withem case/em orderavoid .<p>
<p>For example, valid <strong class="pkg">GAP</strong> input which assigns some user variables whose names start with capital letters may run into errors with a newer version of <strong class="pkg">GAP</strong>java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<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
<p>Note that an expression is
<p>The java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<ul>
<li><plipfunction (see Section a ="chap4_mj.html#815F71EA7BC0EB6F"><span class="RefLink">4..1</pan/a>,</>
<>
<li><p>function literals
</i
<lilip>integer literals (ee <a href".htmlX853DF11B80068ED5">span="RefLink">4/span>/>,/>
</li>
<li><p>integer
</li><p>floating literals(ee <ahref=chap19_mjhtmlX81AA901181CA568Fspan ="RefLink></>/a)<p>
<li<p>loating literalssee Chapter <a href="chap19_mjhtml#81AA901181CA568F"><spanclass="RefLink"19<span<a>,<pjava.lang.StringIndexOutOfBoundsException: Index 128 out of bounds for length 128
</li>
<><ppermutation (seeChapter <ahref=chap42_mjX80F808307A2D5AB8>span class=RefLink></>/>)<p
</li>
<li><p>string literals (see Chapter <a href="chap27_mj.html#X7D28329B7EDB8F47"><span class=
</li>
<li><p><li>
</li>
<li><p/>
</li>
<li><p>record literals (see
</li><p> literals(seeChapter href".html#X7AA1073C7E943DD7"><span="">29/</a>./>
</ul>
<p>Expressions example simple mentioned, can becombined with operators form complex. Of course thoseexpressionscan be further the toform even more . <emoperators> into . <em</>are ="code">/odeclass&;/> codeclass>lt,code=code>/=>gtclass;,andkeyw< <a=".html#X7A274A1F8553B7E6>spanclass""413<aand a =".#X82D39CF980FDBFFA"306/>/>.The <emarithmeticoperators</> are <code class="codecode, ""-/> codeclass">*/>, <code class="code>/> codeclass">mod/>,and <code class="code>^<code seehref.html"><panclass"">414/span></a>operators</mare<code ="keyw<code code=keyw"and</code> and < class="keyw"><code see<a href".htmlX79AD41A185FD7213>span class=RefLink.<span/)</>
<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>The following table lists all operators by precedence, from highest to lowest, and also indicates whether the operator is left associative (aka left-to-right) or right associative (aka right-to-left) or neither.</p>
<div class="pcenter"><table class="GAPDocTable">
<tr>
<td class="tdleft"><em>operator</em></td>
<td class="tdleft"><em>associativity</em></td>
</tr>
<tr>
<td class="tdleft"><em>arithmetic (see <a href="chap4_mj.html#X7B66C8707B5DE10A"><span class="RefLink">4.14</span></a>)</em></td>
<td class="tdleft"></td>
</tr>
<tr>
<td class="tdleft"> <code class="code">^</code></td>
<td class="tdleft">none</td>
</tr>
<tr>
<td class="tdleft"> unary <code class="code">+</code>, unary <code class="code">-</code></td>
<td classtdleftright-left/td>
</tr>
<tr<> following showsvery expressionwith 4 and complex.</>
<td class
<tdclass=tdleft-toright<td>
</tr>
<tr>
<td class="tdleft"> java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
<td class="tdleft">left-to-right
</>
<tr>
<td classtdleft<>comparison (ee<a hrefchap4_mjX7A274A1F8553B7E6>< classRefLink.<span>/></m<td
<td class="tdleft"><tr
java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 5
<tr>
tdclass" <odeclass=code>=/ode, code class""&;>, =""ltcode,< =code"&;<code =">/,code=">;</>,and=keywcode/>
<td class="tdleft">none</td>
</tr>
<tr>
<td class class"<>rithmetic(see< =chap4_mj.html#X7B66C8707B5DE10A">< classRefLink.<span><em/
< =tdleft>td
</tr>
<tr>
<td class="tdclass="tdleft code =code></>/>
<td class="tdleft">right-to-left</td>
</tr>
<tr>
<td class="tdlefttr>
=tdleft
</tr>
<tr ="">rightto-left/d>
<td class>
<td="tdleft>-to-ight<td>
</tr
</table /
</divtr
<td class=tdleft-to</td
<h448 span=Heading</spanh4>
<p>A <em>variable</em> is a location in a <strong class="pkg">GAPtdclass="><em>comparison ( <ahref="chap4_mj#X7A274A1F8553B7E6>classRefLink>.13/pan></m>td
<p>Initially an ordinary variable
<es the of</m functions behave otherexcept areto value the arguments a functioncallsee="chap4_mj.htmlX78C70489791FDF43">span=RefLink4.12/></>.<p>
p variablea name called>/> givenan auniquesee=chap4_mj> class4<><a. em</m a partaprogram. is emglobal</em>that the program, and are scopes range thecode="">function/> keyword denoting the beginning of definitionto the corresponding <code="keywend/>keyword A <emlocal </em>introducesnew,whose giventheargumentlist the codeclass""local/ode of function( ahref"chap4_mjhtmlX815F71EA7BC0EB6Fspan="">4.</span>/>.Usage of identifier aprogram refers the in innermost that this as nameBecause mappingfromidentifiersto is when programisread when is, <trong class=">GAP</strong> is to have <em>lexicalscoping</em> The following shows how identifier refers to variables different points in program text./>
<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 class="tdleft"> <codeclasskeyw<code<td
<p>In those languages a variable denotes </tr
<p>In <strong class="pkg">GAP</strong> on the other hand each variable just points to a value, and different variables can sharetdclass="tdleft"> <code class="keyw">or</code></d>
<p><a id/tr>
<h5.8-1 IsBound</h5
<div class="func"><table class=p>a idX7A4C2D0E7E286B4F" =X7A4C2D0E7E286B4F>/>/p
<p><codeh4>4. <pan="">Variablesspan><h4>
<p>For records and
<p><a id
<h5.- Unbind</5
<div class="func"><table
<p>deletes identifiervar=Arg</var If there is noother variable tothe same as<var class"">ident> was value be by next collectionTherefore <code ="func></ode canbeusedtogetrid ofunwanted largeobjects./>
<p>ForEachvariable has a name also <>identifierem This in scope identifieridentifies a unique variable =".html#X860313A179A5163F">span"46<>/>. A <emscope< partofaprogram . There the <em>globalscope/>thatencloses programtext there thatrangefromthe code=></code> keyword,denotingthe beginningof a definition the corresponding code=keyw"/. >scope in list="localcode of the function ( <ahref=".#815F71EA7BC0EB6F =RefLink4.11<span. of inprogram refers variablethe scopethis as .Becausemapping identifiers variables when is notitexecuted<trongpkg</>isto <>exical/m. Theexample refersdifferentat inprogram./>
<p><a id="X816FBEEA85782EC2" name="X816FBEEA85782EC2"></a></p>
<h4>4.9 <span class="Heading">More About Global Variables</span></h4>
<The majority variablesin class"pkg">GAP/strong defined attheouter level (he scope Theyare usedto functions other created in <strong="">GAPstrong packages thescode<p>
<p>Note that for packages there is a mechanism to implement package local namespaces=function, b,c
<p>Certainspecialfacilities provided for global which not for types variable ( aslocal variablesor function)./>
<p>First, such variables may be marked <em>read-only</em> using <code class="func">MakeReadOnlyGlobal</code> (<a href="chap4_mj.html#X850CE44478254F27"><span class="RefLink">4.9-2</span></a>). In which case attempts to change them will fail. Most of the globaly :=function(y)
<div class="example"><pre>
;
< classGAPpromptgap;/> < classGAPinputglobalb : ;;</span>
<span class="GAPprompt">gap></span> <span;
<span class="GAPprompt">gap>
<span="GAPprompt>&;</span> < =GAPinput>f : ()<spanjava.lang.StringIndexOutOfBoundsException: Index 84 out of bounds for length 84
<span class="GAPprompt">></span> <span class="GAPinput"> if globalb then</span>
< class&;<span ="GAPinput" globali;/>
<span class="GAPprompt">></span> <span class="GAPinput"> else</span>
<spanclass"GAPprompt>gt;/span><span class="GAPinput globali+2/>
<span class="GAPprompt">></span> <span class="GAPinput"> fi;</span>
<span class=
<span<>< idX842B89D4860FD5DB="X842B89D4860FD5DB>a>/>
function ( )
return 1;
end
</pre></div>
<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>code ="func"IsBound>returnsclass">true</code>if variable <varclass="Arg/>points ,and =keyw<code otherwise/p
<pNote the <codeclass"">NamesGVars>(ahrefchap4_mjX876A6EB68745A510>< classRefLink49-/spana>) codeclass"NamesSystemGVars</code>(a ="chap4_mj.htmlX7E604AF579A7BC92"><span class=RefLink>.9-10</pan<a) and<codeclass""NamesUserGVarscode (ahref"hap4_mj.#X870169447AF490D8">span class="RefLink4.11/>/>,deal em namespace><p>
<>< =X7CD3523B84744EB2"name="X7CD3523B84744EB2/>/>
<h5>4.9-1 IsReadOnlyGlobal</h5
<div class="func"><table class="func" width="100%"><tr><td
<p>returns <codeclass"keyw">true</code> if the global variable named the string <var="Arg">name/var> isread-onlyand <code classkeyw>false</code> otherwise (the).</p>
<p><a id="850CE44478254F27" name"X850CE44478254F27"></a></>
<h5>4.9-2 MakeReadOnlyGlobal</h5>
<divclassfunc> class="func ="100"><r>td class="tdleft<code ="func">&8227; MakeReadOnlyGlobal> <var=Arg</var<tdclass">(nbsp; )td<tr<table</>
<
<p>A warning is given if <var
<p><a id="X832AAF13861968BE" name="X832AAF13861968BE"></a></p>
<h54.- MakeReadWriteGlobal/h5java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
<div
<p>marks special facilities areprovided manipulating variables are availablefor other of (such local or function<p>
<p>A warning is
<div
<span class="GAPprompt">gap
17
<span class="GAPprompt"span="GAPprompt"gap;<spanspan class=GAPinput := true;</span>
false
<span class="GAPprompt"< class="GAPprompt"gap;/><spanclassGAPinput>MakeConstantGlobal"globalb");</pan
15
<"gap><span> <span class=GAPinput>MakeReadOnlyGlobal("xx";/pan>
<span ="GAPprompt>gapgt<span spanclass"APinput"xx : 16;<span>
Variable="APprompt>gt<span <span class="GAPinput> returnglobali 1<span>
not in any function
Entering break read-eval-print loop ...
you can 'quit;' to quit classGAPprompt&;/span span="GAPinput"> else/span>
you 'return'after making it writableto continue
<span=GAPpromptgt;/pan classGAPinput>nd;/>
<spanclass=GAPprompt"gapgt<span span =GAPinput">IsReadOnlyGlobal(xx;/>
true
<span class="GAPprompt">gap>return 3 1;
<span class="GAPprompt"/></div>
16
<span ="GAPprompt>&;/span>spanclass"APinput"IsReadOnlyGlobalxx)<span
false
</pre></div>
<p><a id="X847706237E72418F" name="X847706237E72418F"></a><java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<h5-IsReadOnlyGlobal<h5
<div class="func"><table class"< =func" width100%>tr>tdclass=tdleftcode ="func"#8227;IsReadOnlyGlobalcode var=Arg"name/> )</d<td class=">&;unction
<p>MakeConstantGlobal ( <var class="Arg">name</var> ) marks the global variable named by the string <var class="Arg">name</var> as constant.
<p>A warning is given if <var class="Arg">name</var> is already
<p><pmarks variablebystring =""nameasonlyp
h54.5 ValueGlobal/5
<divclass="func>table class="unc width100%">tr< class="tdleft<codeclassfunc8227;ValueGlobal</ode <ar=Argnamevar</td><td class="tdright">( function )</td></tr></table></div>
<p>returns the value currently bound to java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p><a idmarks global named the <var="">namevar read.</>
<h5>.- IsBoundGlobal/>
<div class="func"><table class="java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<> <code="keyw>true/> if avaluecurrently boundto theglobalvariable named bythestring <var class"Arg</varand codeclass=keyw</code>otherwise./>
<p><a idX829A5F0E811F77D3name=X829A5F0E811F77D3<a</>
<h5
<div="func><table ="func"width="100%<><tdclass""><code class=func#227; UnbindGlobal/>(< class"Arg"name>< =tdrightnbspnbsp/>/tr>/>
<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 givenifvar="Arg"name/>was.TheArgname>must,otherwiseerrorraisedp
<p><a id="X7D39D3E17CF49F5B" name="X7D39D3E17CF49F5B"></aut =16;</>
<h5>4.9-8 BindGlobal</h5>
<div class="func"><table class="func" notin anyfunction
<div class="func"><table class=" can '; to quit to outerloop or
<p><code class="func">BindGlobal</code> and <code class="func">BindConstant</code> set the global variable named by the string <var class="Arg">name</var> to the value can;' making it writable continue
<p>This is intended to be<span="GAPprompt"gap;</span><span ="GAPinput">MakeReadWriteGlobal"xx");<span
<p>Caution should be exercised in using these functions
<div class="example"><pre>
<pan classGAPpromptgap></span> <spanclass="GAPinput>xx : 16;;<span
16
<span class="GAPprompt">gap>p><a id"X847706237E72418F" name="X847706237E72418F></></p>
false
<span class="GAPprompt">gap></span> <span class="GAPinput">ValueGlobal("xx");</span>
16
<span class="GAPprompt">gap>divclass="func"><table class="func" width100%><tr><td class"tdleft"><codeclass">‣ MakeConstantGlobal</code>(<varclass="Argname<var>)td classtdright" function&;)</d</tr</></div>
true
<span class="GAPprompt">gap></span
#W BIND_GLOBAL: variable `xx' already has a value
<span class="GAPprompt"
17
<span class="GAPprompt">gap>< classfunctable ="func ="100">>class""><codeclass="func#;ValueGlobalcode>< =Arg/var)/td class"> functionnbsp;<td</r><table></div
true
<span class="GAPprompt">gap></span> <span class="GAPinput">MakeReadWriteGlobal("xx");</span>p< idX823D4BC378395B32="X823D4BC378395B32">/>/>
<span class="GAPprompt">gap&
</></div
<p><a idX876A6EB68745A510"name"X876A6EB68745A510"></a</>
<h5>4.9-9 NamesGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code classjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
pThisimmutable<=".html#7F0C119682196D65"> class126/></> sorted( <ahrefhtml>span="RefLink">2119<spana)list all global names to system.This names of which bound have been and other which never boundbuthave become to system various.</p
<p><a id="X7E604AF579A7BC92" name="X7E604AF579A7BC92"></a></p>
<54.10 NamesSystemGVars/h5
<div="func"><table="func width="100"<><td class="tdleft"< class"">‣ NamesSystemGVars</ode>( )<td<td class"">(&bsp;function&bsp)/></>/table><>
<p>This function returns an immutable
<p><a id="X870169447AF490D8" name="X870169447AF490D8"></a></p>
<h5>4.9-11 NamesUserGVars</h5>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">an style='color: green'>8227; NamesUserGVars</code>( )</td><td class="tdright">( 
<p>Thisfunction returns animmutable sortedlist the variablenames created since the library was read to which avalue currentlybound./p>
<p><a id="X7DF8774F7D542298" name="X7DF8774F7D542298"></a></p>
<h4>4.10 <span class="Heading">Namespaces for <strong class="pkg">GAPdiv class="class="func"=100"<><td=""< =func#;BindConstant/> <ar">name/var>, < class="rg</var><td class"(nbsp;functionnbsp)/>/tr</><>
<p>As mentioned in Section <a href="chap4_mj.html#X816FBEEA85782EC2"><span class="RefLink">4.9</span></a
<p>If in package code a global variable that ends with an <q>at</q>-character <code class="code">
<p>For
< classexample<pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">a@ := 1216
</pre></div>
<p>Then actually the globalspan class"">>/> < classGAPinputValueGlobal(xx/pan>
<p>All other code">><span spanclass"GAPinput>IsBoundGlobal");/>
<pSince earlier releases of< class"">GAPstrong>the>at> <codeclass=">@/code>was not a character (withoutusing backslashes,this small extensionof the language does break anyoldcode./>
<p><a id="GAPprompt">gap></span> <spanclass"GAPinput">xx;/span>
<h4span class="APprompt>gap><span><span class=GAPinput>IsReadOnlyGlobal("xx)</span
<p><code class=<span class="APprompt">gapgt/span <span=GAPinputMakeReadWriteGlobalxx";java.lang.StringIndexOutOfBoundsException: Index 95 out of bounds for length 95
<p><code class
<p><code class="code"> <p><a id"X876A6EB68745A510" name="876A6EB68745A510"/p
p<codeclass""end>/>
<p>A function literal can be assigned to a variable or
<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_mj.html#X85AE1D70803A886C"><span class="RefLink">16.3-1</span></a>)).<<p>Thisfunction returns animmutablesee<a href="chap12_mjhtmlX7F0C119682196D65"><span="RefLink">26<span/a> sorted (see <a hrefchap21_mjhtml">span ="efLink21.</>/a>) listlistofall globalvariable names to systemThis namesofvariableswhichwere but now unboundand othernameswhich neverbeen but becometosystemvariousroutes<pjava.lang.StringIndexOutOfBoundsException: Index 462 out of bounds for length 462
<divdiv classfunctable class=" width=100%>tr>td class""< =func>&8227; NamesSystemGVars</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<="APpromptgap&;</> <spanclass"">fib =function ( n)/>
<span class="GAPprompt">></span> <span class="GAPinput"> local f1, f2, f3, i;</span>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := 1
<span class="GAPprompt">></span> <span class="GAPinput"> for
<spanclass"APprompt"&;/><span=GAPinputf3 +;/>
<span class="GAPprompt">></span> <span class="GAPinput"> f1 := f2;</span> functionreturnsan sorted list global names since library read which value is currentlybound./p>
<span class>a ="X7DF8774F7D542298" ="X7DF8774F7D542298>/a<p
<span class="GAPprompt">></spanh44. span ="Heading>Namespacesfor strongclass"pkg>GAPstrong/>/>
<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], fibp>Asmentioned inSection href="chap4_mj.htmlX816FBEEA85782EC2>< class"RefLink4.</pan></a> all variables share a common.This can relativelyeasily to clashes particularwhenmany class="kgGAP</strong>packages are loaded at thesametime To give package code a way to have a package local namespace without backward compatibilityof the< class="kg</strong, the simple devised>
[1 ,2 ,5 ,13,21,34,55 ]
</pre></div>
<p>Because for
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class
<span="GAPprompt>gt<span <span ="GAPinput"> ifn lt; </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">></> <spanclassGAPinput> ;</spanjava.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
<span
<span class="GAPprompt">gap></span> </pre></div>
[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
<pre<>
<p>Note that the recursive version needs <code
<p
<The exampleshowsone thatthe function< classfuncPosition>(<ahrefchap21_mj.#X79975EC6783B4293<span="RefLink2116-1/span>/>)mightbe encoded demonstrates <q>optional </q .</>
<div class<>aidX815F71EA7BC0EB6F"="X815F71EA7BC0EB6F</>/>
<span class="GAPprompt">gap></span> <span class="GAPinput">position := function ( list, obj, arg... )</span>
<spanclassGAPprompt&;</spanspan="GAPinput> ;/pan
<span class="GAPprompt">></spanp> class"function [< =Arg">-ident{var="Arg">argident/var]/>/>
<span
<span="GAPprompt>><span <span =GAPinput>else<span>
<span class
< class">gt;</> <span class=""> ;</>
<span class="GAPprompt">></span<<codeclass></code/>
<span class="GAPprompt">></span> <span class="GAPinput">
<spanclass="">><span<pan class="APinput> if pos > Length(list) then</span>
<span class="GAPprompt">></span> <span class="GAPinput"> return fail;</span>
<pan="GAPprompt>gt;</span> <pan class=="GAPinput> fi</span
<span class="GAPprompt">></span> <span class="GAPinput">java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<span class="GAPprompt">></span> <span class="GAPinputspan class="GAPprompt"><span> < class""> localf1, f2 ,i;/>
<span="GAPprompt">gtspanspan ="GAPinput>end;/>
function( list, obj, arg... ) ... end class"GAPprompt">&t;</pan classGAPinputforin [..]do>
<span < class"APprompt>gt<span span> := f1 + ;</>
2
<span class="GAPprompt">gap classGAPprompt&t;<spanspanclass=GAPinput" : ;/>
fail
<span class="GAPprompt">gap>span class"">&;</> < classGAPinput> f2<>
fail class=GAPprompt&t<span><pan class="GAPinput"> end;;</span>
</pre></div>
<p> following demonstrates <q>anynumber ofarguments<q scenario</
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span
<;/> <span=""> total;span
<span class="GAPprompt">></span
<span class="GAPprompt">></span> <span class="GAPinput"> for x function (n)/pan>
<span classGAPprompt&;<spanspan="GAPinput">total + x;/span>
<span class="GAPprompt">></span> <span class="GAPinput"> od; class"">&t<spanspan ="GAPinput>returnfib(1)+fib(n2);<span>
<pan=GAPpromptgtspan><pan="APinput"return;</>
<span class="GAPprompt">></span> <span class="GAPinput"> end>
function(l..) .. end
<span class="GAPprompt">gap> ]
6
<span class="GAPprompt">gap></span> <span class="GAPinput">sump>Notethat the recursiveversion needs ""> (var""n/>-<codesteps codeclass">ib(< class"Arg><var/> iterative codeclass">fib</code> only code class="code>var"Arg"></ar-</odesteps. Both not howeverthe function < class">Fibonacci/ode (a =chap16_mj.html#85AE1D70803A886C"<spanclass"163-<span<a)onlyneedsabout< class""Log< =Arg><var>)/code> steps./p>
10
<span class="java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
0
</pre></div>
<p>The
<p>GAP will class"">>/panspan="GAPinput">position list,arg )span
<p>Note that if a ">></pan> <span class=GAPinput>if 0 =Length(arg) then</span>
<>Usingthe < classcode>..</> notation afunction <var class=Argf< only singlenamed argument tells class"GAP</> that whenit encounters <var class=Arg"f<var that should alist out the ofvarclass=Arg"<var.Whatifonewishes dothe<><q:tell< ="kg><stronglist q>unwrappedq passedseveral functionfunction class">CallFuncList/code>(ahref"chap5_mjhtml"><span class="RefLink.-<span>/>) is provided this./p
<p>Also see Chapter <a href="chap5_mj.html#X86FA580F8055B274"><span class="RefLink">5</span></a>.</p>
<p><code class="code">{ <varspan class="GAPprompt"&;<spanspan="GAPinput"> := arg[]</panjava.lang.StringIndexOutOfBoundsException: Index 86 out of bounds for length 86
<p>This is a shorthand for classGAPprompt&;</span> span=""> := pos +1;/>
<) <varArg</var .</code</>
<pspan=">;</span> spanclass=""> ;</>
<spanclassGAPpromptgt;/> < classGAPinput pos/pan
<divclass"xample>prejava.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
<span="GAPprompt>>/span> <span class"GAPinput>(List.100,x}-gt^ ))<span
338350
<span classfail
=GAPprompt&;/> class>Sortlist {,}&t;x>y)</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">list;</span>
[5,3,3, ,1]
<span class="GAPprompt">gap></span> <span classpThe exampledemonstratesthe q>ny number arguments/q scenario.<p>
<span class="GAPprompt">gap></span> <span class
[ 2, 3, 4 ]
<span="GAPprompt>&;/>< =GAPinput> ={ > ;/>
function( ) ... end
<span="GAPprompt">gap>spanspan ="GAPinput>Print(;</>
function ( )
return
end
<span class="GAPprompt">gap></span> <span class="GAPinput">f();</span>
2
</pre></div>
<> <code ="code"{/>andclass"><>maybeomittedfor functionswith :<pjava.lang.StringIndexOutOfBoundsException: Index 118 out of bounds for length 118
<div class="example( l.. . end
<span="GAPprompt>&;/pan spanclass"GAPinput( List1.100]{} &; x^ ) );/pan
338350
<span class="GAPprompt">gap>span="GAPprompt>>/><spanclass=GAPinput>um1,2 ,4)/span
338350
</re<div
<p>When the definition of a function <var class="Arg">fun1</var>
<div
span="GAPprompt">gap&t<span>span""Stack<span
<span class="GAPprompt">></span> <span class<p>Notethat if a function <var class"Arg"f>is as <codeclass="">NumberArgumentsFunction(<var ="Arg"f</var>)/code> returns minus number formal ( the argument)(ee codeclassfunc>NumberArgumentsFunction<code (<ahrefchap5_mj#X877F03F77FD74C98<span class="RefLink>5.1-<span</>))</pjava.lang.StringIndexOutOfBoundsException: Index 372 out of bounds for length 372
span>;span span=""> := ]<spanjava.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82
<span class="GAPprompt">></span> <span class="GAPinput"> return rec(</span>
<span class="GAPprompt">></span> <span class="GAPinput"> push := function( valuejava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<span class="GAPprompt">></span> <span class="GAPinput"> Add
<spanclass="GAPprompt"&;</span span="GAPinput"> ,</span
<span class="GAPprompt">></span> <span class="GAPinput"> pop
< classGAPprompt&; <span ="GAPinput"> return(stack/pan
<span class="GAPprompt">></span
<pan=GAPprompt>/> <span class">)</span
<span class="GAPprompt">></span>p>varclass"arglist/ar>isa(ossiblyempty list argumentslistwhichwouldbevalid anormal GAPfunction isalsovalidhere( variadicarguments)<p
<span class="GAPprompt">gappThe givescouple of examples a typicaluseof a function/>
<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 );< class"">gap;</pan> < classGAPinput( List.100] { > x2))<span>
< class">&;/> span =GAPinput>.ush ;Bpush ) 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,3,4]
<p><a id="X78C70489791FDF43" name="X78C70489791FDF43"></a></p>
<h4>4.12 <span class="Heading">Function Callsreturn
<p><a id="X80B93A9C7E0A57F4" name="X80B93A9C7E0A57F4"></a></p>
<
exprvarvar=Arg-exprvar> .])/><p>
<p>The function call has the effect of calling the function <var class=java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<pFirst class"pkg"GAP/> evaluates <var="Arg">-var/varUsually var=">unction-var/var variable and < class=pkg"></strong nothing than the of variable It is allowedthough that classArgfunction-var/> is morecomplex expression,such asa to element a list( Chapterahref"hap21_mjhtml#7B256AE5780F140A"<span"RefLinkspan<a>)<codeclass="code> ="Arg">list-var<var>[<var class"">-expr/var</ode or to acomponentof a recordsee Chapter a href=chap29_mj#X7AA1073C7E943DD7<panclass"29<span/>)<ode class="code"<var class=Arg>-var</var>.<varclass"Arg><varcode>.Incasestrong"">GAPstrong tests whether value afunction.If is, <strong="pkg">GAP/> signals errorp
<p>Next<strong class=""GAP</strong checksthe ofargumentsvar"rg"arg</varagrees with the of<>formal</em> as given the definition they not <strong="">GAPstrong>signals error An is case the has length listwhich is by <codeclasscode..<code the argument In case must atleast manyactual as are <embeforethe argumentem can anylarger (seea ="chap4_mjhtmlX815F71EA7BC0EB6F"><span class="RefLink">.11/><a examples.</p>
<p>Now p> the of <var="">fun1> is inside function classArg</var>, <strong="">GAP<strong all identifiers the function< class"rg"<var are of anargument a localof classArg> thecorrespondingvariable.This set bindings iscalled environmentof the < classArg</varWhen classArg</> called,its is inthisenvironment.The followingimplementation of a simple uses. Valuescan pushed thestack then later be off. The interestingthing hereis the <code class=code><code and =code><code thereturned code="">Stack> access the variable class=code>tack/>of classStack<code>.When < classcode></>iscalled, a new variableforthe identifier <<code ="code></code created the function definitions <code class=code/ and< class=code"></code then as of <code class""></code statement reference""stackcodeboundthis. two code ="code>A<code codeclass""/>donot ,because callof< =code>tack/code variable for<code class="odestack><>
<p>Next the arguments <var class="Arg">arg-expr</var>s are evaluated from left to right, 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. An exception again occurs if the last formal argument has the name <code class="code">arg</code>. In this case the values of all the actual arguments not assigned to the other formal parameters 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
<>Now the body of the function,which a statement,is executed. Ifthe of of formal or locals inthebody ofthe it to new that allocated this argumentor localand to the value of this variable.</>
<> during executionof bodyofthefunctiona< class"keyw">return</code statement with an (see< href=chap4_mjX812C6ABC7A182E9E"<spanclass"RefLink">.5-9</span></a>>) is executed,executionof the body is terminated and value of the function call is the value ofthe expression of the <code class="keywreturn>.If during the of the body <ode ="keyw"</>statement an is executionthe isandfunction doesproducevalue which wethisaprocedurecall< href.html#X825803DE78251DA6">spanclass"RefLink>152<spana) the ofbody of code=keyw"return/ode statement, function call again produces value, again talk about a procedurecall.</>
<div class="example"><pre>
<span class"GAPprompt">gap;/> <span=GAPinput>( 11);/span>
89
</pre></div>
<> example acall function class">Fibonacci/> <a ="chap16_mj#X85AE1D70803A886C"< class="">16.3-1<span></> actual argument <code class="code>11</odethe one acallthe <code =func</ href.htmlX835F48248571364F">spanclass="">397-/span>/a)where secondactual argument is anotherfunctioncall./java.lang.StringIndexOutOfBoundsException: Index 455 out of bounds for length 455
<div="example"><re>
<span class="GAPprompt">gap&span="GAPprompt">gtspan>span="GAPinput"> := function)/>
</pre></div>
idX867D54987EF86D1Dname=X867D54987EF86D1D>/>/>
<h5>4.12-2 <span class="Heading">Function Call With Options</span></h5>
<p><code class="code"><span="GAPprompt">gapgtspan span="GAPinput">;/>
<p>As well as class"gapgt;/><span =GAPinput">B.( 4 ; .(5 );Bpush(6 ;/span
<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
<p>Options are evaluated1
<p>The following example a call <code ="func">Size</ode<a ="chap30_mjhtmlX858ADA3B7A684421">< classRefLink30.-6/></a the <code="code></ode> ( thevalue<code class=keyw></ode>) code class="code">tcselection</code with the string code class="code""</> value<pjava.lang.StringIndexOutOfBoundsException: Index 366 out of bounds for length 366
<div class="example">pre<div
<span class="GAPprompt">gap></span> <span>This feature used, itsin class>AP> is very/>
</pre></div>
<p>
<div class="example">p< ="X80B93A9C7E0A57F4"name"<a><p>
<span class="GAPprompt">gap></span>412- span class"Function CallWithArguments/pan<h5>
<span class="GAPprompt">gap></span> <span class="GAPinput">Size( fpgrp );</span>
<pan ="GAPprompt">ap<spanspan"APinput">PopOptions);<span>
</pre></div>
<p><em>Note</em> that java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p><a id="X7A274A1F8553B7E6" name="X7A274A1F8553B7E6"></a></p>
<h4 span class=Heading"Comparisons/span></4java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
<p><code class="code">java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p><code class="code"><var class="Arg">left-expr</var> <
<p>Theoperator< classcode">=/code tests for ofitstwooperands and evaluates to<codeclass=keyw>true/> they are equal and to <code ="keyw<code otherwiseLikewisecodeclass">lt;;</code>testsforinequalityof its two operands. For eachtype objects definition of equality is given in therespective chapter.Objects different families (see<a href="chap13_mj.#X846063757EC05986< =RefLink">13.1/span></>) are never equal, ie.,< class""><code evaluates inthis case to <code ="keywfalse, < ="">ltgt<code evaluates to class=keyw</code></>
<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
<p><code class="code"><var class="Arg">left-expr</var> <= <var class="Arg">right-expr</var></code></p>
<spanclass"GAPprompt">ap&;/> < classGAPinputFibonacci 11)<span>
<p><code classpre><div
<p>Note <codeclass=code>ltcode a <em>total</m>of objects which be for to a list can using =func</>(a ="chap30_mjhtmlX79CA175481F8105F"><span="RefLink">051</pan>) For, ispossibleto permutation withcode class="code">&t;</code>inatotal ofall groups thisis compatible the of a subgroup)/>
<p>Only for the following kinds
<p> other, < class"">GAP> does>not> provide an via ="code"ltcode Thefor thattotalof< =pkg<strong would maintainnewofare, andatotal hardlyitsgenerality/>
<p>However, for objects in the filters listed above, the ordering via <code class="code"><pan="GAPprompt">gap&t;/> <spanclassU, ;<span
<p>Of course it would in principle be
<>Comparisonoperators includingthe <code=keywin> (seeahrefchap21_mj.#X808A207182B2F84F>span="RefLink">18/span>/a>)) not Hence it is allowed to writecode class"code"><var class=Arga</> = <="Arg">b/> &;><varclass""c/ar <var="d/>/ode> you must use<ode class""(varclass"Arg<var<var="Arg">b/var> lt; (var="">c/> = < classArg<var><codeinstead.The operators higherprecedence thelogical operators see href"chap20_mj.htmlX79AD41A185FD7213">span class=RefLink">20.</pan<a>,butlowerprecedence operators ( a href".html#X7B66C8707B5DE10A"<span class="RefLink"414/></a>) Thus, for , <codeclass"">varclass="Arg<var< =Arg/>=<var"rg><var and <var =Arg><var</ode>isinterpreted codeclass="ode"(< ="Arga/> *< class"rg></ar>) = < =""><var>) and <varclass="Arg/>/></pjava.lang.StringIndexOutOfBoundsException: Index 1140 out of bounds for length 1140
<p>The following<p>code=""<ar""function<var =Arg"arg<var[ <arclass="Arg"argexpr/ar,...] < ="">option-expr</var> [,<arclass"">option-expr</var>, ...])</code><p>
<div
<panclass"GAPprompt">gap;/><span""2 2 9=Fibonacci)<span
rue
</pre></div>
<
<p>a ="X7B66C8707B5DE10A" nameX7B66C8707B5DE10A><a><p>
<>. span> <span>
<p><code class="code">+ <var class="Arg">right-expr</var></code</re</div>
<p><code class="code">-
<p><codeclass"code"><var classArg>left-exprvar> +<var class"rightexpr<var</ode></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>
<p><code class"ode"<varclass"rg>left-expr</var>/ <var ="">rightexpr<var</code></p>
<p< class=code< =Arg-expr>modclass">right-</var>/code><p
<classvar=">-expr</var>^<var ="Arg>-<varcode>/>
<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
<p><code class="code"><var class="Arg">a</var> + <var class="Arg">b</varp><code class="code">varclass"rg">left<varlt <var=Argright<varcode>/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>
pcode="">var="Arg></var> <var class=Arg"><varcode the multiplication of"Arg">a<var themultiplicative inverse of < class"Arg></>./pjava.lang.StringIndexOutOfBoundsException: Index 200 out of bounds for length 200
<> classcode"<var class="Arg"></ar mod <var class"Argb</ar/code>,for or rational operand <var class"rg">a<//varand for-zerointeger right <var="Arg">/varis followsIfvar"">/>and class">b<var are bothintegers, < class="code>var""><varmodvar"Arg></></code>is the integer<var class"">r</ar>in theintegerrange <code class"">0 . |varclass="rg<var-1code satisfying < classcode>var="Arg"><var =< class"Arg">r/var>+< classArg</ar>var ="Arg"></var><code> for some integer <var class"Arg"">q</var> (where the operations occurring their usual meaning over the integers, of course)</p>
<p>If <var class="Arg">a</var> is a rational number and <varp>codeclasscode>&t;/code> denotes less than,<code class"ode">lt=/codeless ,codeclasscode>gtcode greater andcodeclasscode">><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>With the above definition, <code class="code">4 / 6 mod 32</code> equals <code class="code">2 /
<p><em>Note:</em> For rational
<p><code class="code">+</code> and <code class=
<p><code class="code
<p>The <em>precedence</emjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p>The <em>associativity</em> of the arithmetic operators is as follows
<p>The arithmetic operators have
<div class="example"><</pre>/div
<span class="GAPprompt">gap></span> <span class="GAPinput">2 * 2 + 9; # a very simplep>Forthe underlying the operators above see a="chap31_mj.html#X7B3BC7BA7BB2646D><span class=RefLink>31.<span</>.<p>
13
</pre></div>
<p>Forother operations, and for the underlyingoperations of the introduced above, see<ahrefchap31_mjhtmlX7A2914307963E370"><pan class="efLink31.12</><a></p>
<p><a id="X8543285D87361BE6" name="X8543285D87361BE6"></a></p>
<h4<p><code class"code">+ <var="Arg">rightexprvar></code>/p>
<p><strong="kg">GAP</trongprogramsconsist a sequence ofso <emstatements/m>.Thefollowingtypesstatements:</>
<ul>
<li><p>ssignments(see Section<a href="chap4_mj.html#X7E6A50307F4D3FAE""><span class="RefLink">4.15-1</span</>)<>
<>
<li><p>Procedure
</li>
<li><><odeclass="keyw"if/> statements( a hrefchap4_mj.html#X875000188622700D<span classRefLink>415-3</span/>)</
</li>
<li><p><code class="keyw">while</code> loops (see Section <a href="chap4_mj.html#X87AA46408783383F"><span class="RefLink">4.15-4</span></a>),</p>
</li>
<li><p><code class="keyw">repeat</code> loops (see Section <a href="chap4_mj.html#X8295CBF47FAA05C9"><span class="RefLink"<p>The arithmetic are <code class"code"></code>, <"-/> <ode class="ode/ode< classcode><code, < classkeyw<code,andclass"><code.The meanings (semantics) of those operators generally depend on the typesof the operands involved, and they are definedinthevarious chapters describing types. However basically the meanings are as follows.</p>
</li>
<li<p>code class"code"><varclass"rg"a<var < class"rg">b</var/> denotes the additionof additive elements <var class="Arg">a</arandvar"Arg"b/>./p>
</li>
<li><p><code class="keyw">break
</li>
<li><p><code class="keyw">continue</code> statements (see Section <a href="chap4_mj.html#X7CCBA2247AA366BD"><span class="RefLink">4.15-8</span></a>), and</p>
</li>
<li><p><code class="keyw">return</code> statements (see Section <a href="chap4_mj.html#X812C6ABC7A182E9E"><span class="RefLink">4.15-9</span></a>).</p>
</li>
</ul>
<p>They can <>If < class"Arg""avar isrational numberand< class""b/ar> nonzerointeger and<code class="code"<arclass"Arg"a/var> == <var class=Arg><var> varclass=Arg>n</var></code>where < class="Arg">m</> and <var class="Arg/> are integerswith < class"">n</var, thencodeclass=code< class"">a</ar <var="Arg"b<var>/> is integervar="Arg">r<varin integer <code class="code>0 ..|var class="Arg></>|- 1/>suchvar=Arg<var congruenttoclass">< class="Arg"></var><var class"Arg"><var<code> modulo <var ="Arg></> < =Arg<var <=Arg<var var="">b/></code> isis called the> inverse/q> of class"">n</ar modulo <varclassArg>/var> (A pair of integers is said to be>coprimeem (or <>relatively</em)if greatest common divisor is.
<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
<p>Using expressions as statements is treated as syntax error.</p>
lass""><pre
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 7;;<p><><codeclass"code"></code> andcodeclass=code"-</code>can also be used as unary operations. The unary<code class="code"</> is ignored. Theunary<code class="-/odereturnsadditiveofoperandover is to multiplicationbycode classcode"-1</code></>
<gt0 then k =16/;fi/span>
Syntax error: := expected
if i <><Theem>precedence<em of operatorsis follows The operatorcode class=""<code the precedence followed by the unary <code="code"></> and <code class=code></ode which followed the operators<ode=code>*/ode<codeclass">//code,and code class="keywmod/code> the binary < classcode><code and<codeclasscode>-/> havetheprecedenceThat that expression< >2^ -2 * + <code>is as ="">(-( ^ (-) * 3) + <code.If doubt parentheses clarifyyour intention.<p>
gap>
</pre></div>
<p> youcan see the example this doesin particular those who used to languages wherecode class=""></ode of< classcode">=<code>denotes assignment.</>
<p>Empty statements are permitted and have no
<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<pan class="GAPprompt">gap></> <span class=GAPinput>2 2 + 9 #avery arithmetic<span
<p><a id="X7E6A50307F4D3FAE" name="X7E6A50307F4D3FAE"></p>Forotherarithmeticoperations, and for the underlying ofthe operators above seea hrefchap31_mjhtmlX7A2914307963E370><span="RefLink">31.12/></a>.</>
<54.151 <pan class="Heading">Assignments</span></h5
<p><code class="code"><var class></h4
<p>The <em>assignment</em> has the effect of<p><strongclass"pkg">GAP<strong programs consist a sequence socalled <em</em> The following types statements exist</p>
<p>The variable <var class="Arg">var
<p>Note that variables do
<div class="example"><pre>
<pan="GAPprompt">gap><span> <pan="GAPinput>data: (numbers= 1, ,3])<spanjava.lang.StringIndexOutOfBoundsException: Index 105 out of bounds for length 105
rec
<span><p>codeclass="keyw">while/code> loops Section< href=chap4_mj.#X87AA46408783383F"<span class=RefLink>.15-4/span>/a)</p
rec
<span class="li>
rec := 1 4, 3] stringstring
</pre></div>
<>If expression <varclass="Arg"expr</var is a function callthen this mustreturn a value. If does not return a valuean errorissignalled and enter a break (see <a href="chap6_mj.html#X8593B49F8705B486"><span="RefLink">6.4</span</>)As youcan the break loop with<code class="code">;</code>.If you <code class="ode">return <var class="Arg">return-expr/var</code> the valueoftheexpression <var classArgreturn</varis thevariable,and continues after the assignment.</p>
<iv="example">pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">f1:= function( </li
<span class="GAPprompt">gapgt;</span> <span class="GAPinput">f2 function( x) return f1x) end<span>
<span class="GAPprompt">gap></span
value 4
Function Calls<i<p><code class="eyw">eturncode>statements Sectiona hrefchap4_mj.html#X812C6ABC7A182E9E"< class="RefLink">4.-</pan></a>)</p>
return f1( x );
called from
<function>( <arguments> ) called from read-eval-loop
Enteringbreak-eval-print loop ...
you can 'quit;'
you can supply one by 'return <value>;' to continue
<span class="GAPbrkprompt">brk></<p>Statements unlikeexpressions no. They executed toproduce an. For an has effect assigning to a variable a <codeclass="keyw">for/code> loophas theeffect of a statement forall elements in aa list and soon. We talk about<m>evaluation/m of expressions about <em>execution/em> of toemphasize this./p>
"hello"
</pre
<p>In the above example, the<div class="example"><prejava.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
<p>>/span> span="GAPinput"if <> 0 thenk = 16/;fi<span
<h5>4.15-2 <spanif i <> 0 thenk=16//i; fi
<p><code class="code"><var class="Arg">procedure-var</var>( [<var class="Arg">arg-expr/pre</div>
<p>The <em>
<p>A <em>function</em> does return value but notproduce a side effect. As a conventionthe of a functionis a noun,denoting the functionreturns e..,<ode="code">"Length</code>, <code class="code">"Concatenation"</code> and <code class="code">"Order"</code>.</p>
es return avalue but some effectProcedures are called for this. As convention the name of procedure is a verb what the procedure, e.., <ode="code""Print</code, code class="code>Append/code> nd<code class="code">"</code>./p>
<divclassexample"><pre>
<span class="GAPprompt">gap></span> <span class
span"GAPprompt>gapgt;</span <panclass="GAPinput> : [ 1,2 ];</pan>
<span class="GAPprompt">gap></span> <span class="GAPinput">Append( l, [3,4,5] );
<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_mj.html#java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<p><a id="X875000188622700D" name="X875000188622700D"></a></p>
<h5>4153 <span="Heading>If</span>/>
<p<code class=""> <var="Arg">-expr1</var> <="Arg">statements1<var{elif <var="Arg">ool-</var then <var classArgstatements2</ar> [elsevar class="Arg>statements3</var>] fi;</code>/p>
<p>The <code class="keyw">if
<p>First the expressionspan="GAPprompt"gapgtspan <spanclassGAPinput">data= rec( numbers: [ 1, 2, ] ;</pan>
<p>Otherwise the expressions <var class="Arg">bool-expr2</var> following the <code class="keyw">elif</code>rec( := [ 2 3 ]string : "" )
<p>If the <code class="keyw">if( numbers= [ 1,4 3 ] string := "" )
<p>Since the <code class="keyw">if</code> statement is terminated bypIf expression < class=Argexprvarisa functioncallthis return value If functiondoes return value error signalledand enter break ( < hrefchap6_mj.htmlX8593B49F8705B486"<spanclass"RefLink6.<span/) As can the loop< classcode>quit/> If entercodeclass">return<var class="Argreturn<varcode the of expression< classArgreturnexpr/aris to variable executioncontinuesafterassignment<p>
<div class="example"><pre>
if expr1 then if expr2 then stats1 else stats2 fi
</pre></div>
<p>the <code class="keyw">else</code> part belongs<span class="GAPprompt">gap&t;</pan> <span classGAPinput>f2:= function(return( x ); end<span
<div class="example"><pre>
if expr1 then if expr2 then stats1fi; stats2 fi
</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="Entering break read-eval-print loop ...
<div class="example"span class=GAPbrkprompt>brk></pan<span classGAPinput"return"hello";<span>
abs := ifhello
</pre></div>
<p>which would, even if legal>n the above,the <code="code"f2code> callscode class="ode>f1</code <code class="code"><code>, and since <code class=code"f1code return butprintsline< classcode">alue: ../></>) the <code class="keyw></ode> statement of <code class="code"f2/> cannotexecuted The message thatis toan valueand returned <codeclasscode>hello/> is used <code class="code>f2<code> instead ofthe missing return value of <code class="code>f1</code.</>
<p>If one of the expressions <var class="Arg">bool-expr1</var>, <var class="Arg">bool-expr2</var> is
<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 &p><code class="code"><var class="rg">procedure<var [<var=Argargexprvar,var=Argarg</var .] ;code/p>
<span class="GAPprompt">></span> <span class="GAPinput"> s := 1;</span>
<spanclass="GAPprompt">></> <spanclassGAPinput elif &t;0then</span
<span class="GAPprompt">></span> <span
<span class="GAPprompt">>>A <mfunctionem> does a valuebut does produce a side. As convention the name a function a noun denoting what functionreturnse..,codeclass="">"ength"/code,<codeclasscode""Concatenation<code and <code classcode>Ordercode./pjava.lang.StringIndexOutOfBoundsException: Index 291 out of bounds for length 291
<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></>
<p<a ="X87AA46408783383F" name"X87AA46408783383F"></a>/p>
<5>4.154 <span class="">While></h5>
<p><code class="code">while <var classp>Thereare a fewexceptions of class=pkg>GAP</strong functions do both return a value andproduce some. An is< classfunc</code> (a hrefchap21_mj..html#X87287FCA81E2B06A"><span ="RefLink>2118-</pan/>)whichalistreturns corresponding of the entries</p>
<> code classclass"eyw">while/ode loopexecutesthestatementsequence classArg">statements</var> whilethe 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
<p>hedifferencebetween the <code class"keyw>while</code> loop and the <code class="">epeat/>< ="eyw>ntilcode loopsee hrefchap4_mj.html#X8295CBF47FAA05C9">span class=RefLink>4.15-5</span></a>)is the varclass"Arg>statements</var in thecodeclasskeywrepeat</code> class="keyw">until/code> loopare at least, while <var class="Arg">tatements/> in thecodeclass="keyw"while/code>loop notexecuted all var=Arg>bool-expr/var> iscodeclasskeywfalsecode> at thefirst 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
p following showscodeclass="while/code loop that sums up the squares <span class=="SimpleMath">\(^2, 22^2,\ldots)<span> until the sum exceeds <span class="impleMath>(200\</pan>.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := 0;; s := 0;;</span>
>while&t;200 do<span
<span
span="GAPprompt">>/span class odspan
<span class="GAPprompt">gap></span> <span class="GAPinput">s;<p>incethe<codeclass"keyw">if</code statement isterminated the <ode classkeywfikeywordthere is no questionwherean ="keyw></>part ,ie,< class="">GAP</> has no <q>dangling else</> In</p
204
</pre></div>
<p</re/div>
<<p>thecode class="keyw>else<code>part belongstothe second< class=keyw"if</code> statementwhereas in<p>
<h5>4.15-5 <span class="Heading">Repeat</span></h5>
<p><codeclass"ode>repeat <var class="Arg">tatements</var> until <var class="rg>bool-</var<code/p>
<p>The class"elsecode belongsto the first <=keyw>if/code> statement</p>
<> <var=Argstatements<var executed <var="Arg">bool-expr<var evaluated itevaluates to <odeclass=keyw</code> <code="keyw"repeat> terminates and statement immediately following <="">repeat> loop is executed next if evaluates to <code class"keyw">false</code the wholeprocess again the of the <var classArg>tatements</>.</p>
<p>The difference between the <code class="keyw">while</code> loop (see java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
pIfvarclass="Arg>bool--expr</var> doesnot evaluate to<code class=keyw"true>orcodeclasskeyw">false<code> an signalled a break loop(see <a href="chap6_mj.htmlX8593B49F8705B486>spanclass"RefLink>.4</pan</) isentered. As usualyoucanleavethe break with <odeclass"code>;</code>. If you <code class="code">return true;</code> executioncontinueswith the statement immediately followingthecode class"eyw"repeat/code> loop.Ifyouentercode class"code">returnfalse<code,execution at<var=">statements</var>,afterwhich the next evaluation of<var class="rg>boolexprvarmaycauseanothererror<p>
<> <codeclasskeyw"repeat/code> loop inthefollowingexample has the same purpose as the <code class="keyw">while/code> loop in the precedingexample namelyto sum up the squares <span class=SimpleMath>(1^2, 2^2,\ldots\)</span> until the sum exceeds <span class="SimpleMath>(200\)/span></p
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span classGAPprompt>/pan class"i: 10;<span>
<span class="GAPprompt">gap></span> <span class="GAPinput">repeat</span>
<spanclassGAPprompt&;</span><span class"APinput"> i : i + 1;s:=s +i^;/span
<span class="GAPprompt">></span> <span class="GAPprompt>gt;/span> <span class="GAPinput"> s = 1;</spanjava.lang.StringIndexOutOfBoundsException: Index 76 out of bounds for length 76
<span="GAPprompt">>/span span=GAPinputs<span
204
</rediv
<g code="keyw">break/code> see <a href"chap4_mj.html#X7B60C6127E183021"><spanclass="RefLink"4.5-7</</span</a></p>
<p>a ="E777867638A" nameX78783E777867638A"></a>p>
java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 48
<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-
<p>The statement sequence <var class="Arg">statements
<p>The execution of the <code class="keyw">for</code> loop over a list> differencebetween the classkeyw<codeloop the code="keyw></code code class="keyw">ntil/ode (ee ahref"hap4_mjhtmlX8295CBF47FAA05C9span="4.5-5/span></>) is that the < class="rg></> in the<code class"">repeat/code> < class"eyw">untilcode>loop executedat once while var=Arg">statements/var> in the <code class="keyw"while</code> loop arenotexecutedatallif <var class="Arg"boolexpr</> is <code class"keyw">false<code atthe first iteration.</p>
<div class="example"><pre>
loop_list=list;
loop_index := 1;
while loop_index <= Length(loop_list) do
variable[];
statements
loop_index=loop_index + 1;
od;
</></div
<st</q> andq>loop_indexq>are variablesfor eachcode="keyw">for/code>loop, i.e., these variables of different<codeclasskeyw>for</code>loops not interfere each other<pjava.lang.StringIndexOutOfBoundsException: Index 243 out of bounds for length 243
<p>The list
<p>< class"">for <var class"Arg">ariablevarin[var="Arg">fromvar.var="Arg">to>] do < class=Arg>tatementsvarodcode>/p>
<p>corresponds to the more common</p>
<p><code class="code">for <var class="Arg">variable</var> from <var<p><a id"X8295CBF47FAA05C9" nameX8295CBF47FAA05C9"<a</>
<p>java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
<div class="example"><pre>
<spanclass=GAPprompt>><span> <pan class="APinput>s := 0;;<span>
<span class="GAPprompt">gap></span> <span class="GAPinput">for i in [1..100] do</span>
<pan="><span spanclass"GAPinput> s : s + i;/span>
<span class="GAPprompt">></span> <span class="GAPinput">od;</span>
<spanclass="GAPprompt"gapgt;</span<span class=GAPinput>;<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<p>If <var="Arg>bool-exprexpr<var>does evaluate to <codeclass"keywtruecode> =keywfalseerror and=code>uit;</code>.Ifyouentercodeclasscode"returntrue;/>, continues with the next statement immediately following the <code class="eyw"repeat</code> loop. If you enter <code class="code"return false;</code>, execution continues at <var class"">tatements/var> after which the evaluation of<var class="Arg-</varmayanother./p>
<div class="example
<spanclass="GAPprompt">gapgt<span> <pan="GAPinput>l :[1,,3,,5, 6 ];</span>
<span class="GAPprompt">
<span class="GAPprompt">></span> <span class="GAPinput"> Print( i, <span class="GAPprompt">ap;/> < class:0;s : 0;</span
< class"&;/span span ="APinput"if 2 then Add(l,3*i/2) fi</pan>
<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 ,,9,9 ]
</pre></div>
<p>Note in the following example that
< class"<
<span class="GAPprompt">gap></span> <span class>aidX78783E777867638Aname=X78783E777867638A></>/> | |