Debian Codegraph @

doc/tut/chap4.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <title>GAP (tut) - Chapter 4: Functions</title>
9 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
10 <meta name="generator" content="GAPDoc2HTML" />
11 <link rel="stylesheet" type="text/css" href="manual.css" />
12 <script src="manual.js" type="text/javascript"></script>
13 <script type="text/javascript">overwriteStyle();</script>
14 </head>
15 <body class="chap4" onload="jscontent()">
16
17
18 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
19
20 <div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap3.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap5.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap4_mj.html">[MathJax on]</a></p>
23 <p><a id="X86FA580F8055B274" name="X86FA580F8055B274"></a></p>
24 <div class="ChapSects"><a href="chap4.html#X86FA580F8055B274">4 <span class="Heading">Functions</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4.html#X7D81680E7BB09F45">4.1 <span class="Heading">Writing Functions</span></a>
26 </span>
27 </div>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4.html#X801EE07E839B31B2">4.2 <span class="Heading">If Statements</span></a>
29 </span>
30 </div>
31 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4.html#X797E24D67BB804A2">4.3 <span class="Heading">Local Variables</span></a>
32 </span>
33 </div>
34 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4.html#X7981E4197F7113EA">4.4 <span class="Heading">Recursion</span></a>
35 </span>
36 </div>
37 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4.html#X85B6E21781804BBB">4.5 <span class="Heading">Further Information about Functions</span></a>
38 </span>
39 </div>
40 </div>
41
42 <h3>4 <span class="Heading">Functions</span></h3>
43
44 <p>You have already seen how to use functions in the <strong class="pkg">GAP</strong> library, i.e., how to apply them to arguments.</p>
45
46 <p>In this section you will see how to write functions in the <strong class="pkg">GAP</strong> language. You will also see how to use the <code class="keyw">if</code> statement and declare local variables with the <code class="keyw">local</code> statement in the function definition. Loop constructions via <code class="keyw">while</code> and <code class="keyw">for</code> are discussed further, as are recursive functions.</p>
47
48 <p><a id="X7D81680E7BB09F45" name="X7D81680E7BB09F45"></a></p>
49
50 <h4>4.1 <span class="Heading">Writing Functions</span></h4>
51
52 <p>Writing a function that prints <code class="code">hello, world.</code> on the screen is a simple exercise in <strong class="pkg">GAP</strong>.</p>
53
54
55 <div class="example"><pre>
56 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sayhello:= function()</span>
57 <span class="GAPprompt">&gt;</span> <span class="GAPinput">Print("hello, world.\n");</span>
58 <span class="GAPprompt">&gt;</span> <span class="GAPinput">end;</span>
59 function( ) ... end
60 </pre></div>
61
62 <p>This function when called will only execute the <code class="code">Print</code> statement in the second line. This will print the string <code class="code">hello, world.</code> on the screen followed by a newline character <code class="code">\n</code> that causes the <strong class="pkg">GAP</strong> prompt to appear on the next line rather than immediately following the printed characters.</p>
63
64 <p>The function definition has the following syntax.</p>
65
66 <p><code class="keyw">function</code><code class="code">( <var class="Arg">arguments</var> ) <var class="Arg">statements</var></code> <code class="keyw">end</code></p>
67
68 <p>A function definition starts with the keyword <code class="keyw">function</code> followed by the formal parameter list <var class="Arg">arguments</var> enclosed in parenthesis <code class="code">( )</code>. The formal parameter list may be empty as in the example. Several parameters are separated by commas. Note that there must be <em>no</em> semicolon behind the closing parenthesis. The function definition is terminated by the keyword <code class="keyw">end</code>.</p>
69
70 <p>A <strong class="pkg">GAP</strong> function is an expression like an integer, a sum or a list. Therefore it may be assigned to a variable. The terminating semicolon in the example does not belong to the function definition but terminates the assignment of the function to the name <code class="code">sayhello</code>. Unlike in the case of integers, sums, and lists the value of the function <code class="code">sayhello</code> is echoed in the abbreviated fashion <code class="code">function( ) ... end</code>. This shows the most interesting part of a function: its formal parameter list (which is empty in this example). The complete value of <code class="code">sayhello</code> is returned if you use the function <code class="func">Print</code> (<span class="RefLink">Reference: Print</span>).</p>
71
72
73 <div class="example"><pre>
74 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(sayhello, "\n");</span>
75 function ( )
76 Print( "hello, world.\n" );
77 return;
78 end
79 </pre></div>
80
81 <p>Note the additional newline character <code class="code">"\n"</code> in the <code class="func">Print</code> (<span class="RefLink">Reference: Print</span>) statement. It is printed after the object <code class="code">sayhello</code> to start a new line. The extra <code class="keyw">return</code> statement is inserted by <strong class="pkg">GAP</strong> to simplify the process of executing the function.</p>
82
83 <p>The newly defined function <code class="code">sayhello</code> is executed by calling <code class="code">sayhello()</code> with an empty argument list.</p>
84
85
86 <div class="example"><pre>
87 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sayhello();</span>
88 hello, world.
89 </pre></div>
90
91 <p>However, this is not a typical example as no value is returned but only a string is printed.</p>
92
93 <p><a id="X801EE07E839B31B2" name="X801EE07E839B31B2"></a></p>
94
95 <h4>4.2 <span class="Heading">If Statements</span></h4>
96
97 <p>In the following example we define a function <code class="code">sign</code> which determines the sign of an integer.</p>
98
99
100 <div class="example"><pre>
101 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sign:= function(n)</span>
102 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n &lt; 0 then</span>
103 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return -1;</span>
104 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> elif n = 0 then</span>
105 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 0;</span>
106 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> else</span>
107 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
108 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
109 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
110 function( n ) ... end
111 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sign(0); sign(-99); sign(11);</span>
112 0
113 -1
114 1
115 </pre></div>
116
117 <p>This example also introduces the <code class="keyw">if</code> statement which is used to execute statements depending on a condition. The <code class="keyw">if</code> statement has the following syntax.</p>
118
119 <p><code class="keyw">if</code> <var class="Arg">condition</var> <code class="keyw">then</code> <var class="Arg">statements</var> <code class="keyw">elif</code> <var class="Arg">condition</var> <code class="keyw">then</code> <var class="Arg">statements</var> <code class="keyw">else</code> <var class="Arg">statements</var> <code class="keyw">fi</code></p>
120
121 <p>There may be several <code class="keyw">elif</code> parts. The <code class="keyw">elif</code> part as well as the <code class="keyw">else</code> part of the <code class="keyw">if</code> statement may be omitted. An <code class="keyw">if</code> statement is no expression and can therefore not be assigned to a variable. Furthermore an <code class="keyw">if</code> statement does not return a value.</p>
122
123 <p>Fibonacci numbers are defined recursively by <span class="SimpleMath">f(1) = f(2) = 1</span> and <span class="SimpleMath">f(n) = f(n-1) + f(n-2)</span> for <span class="SimpleMath">n ≥ 3</span>. Since functions in <strong class="pkg">GAP</strong> may call themselves, a function <code class="code">fib</code> that computes Fibonacci numbers can be implemented basically by typing the above equations. (Note however that this is a very inefficient way to compute <span class="SimpleMath">f(n)</span>.)</p>
124
125
126 <div class="example"><pre>
127 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fib:= function(n)</span>
128 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n in [1, 2] then</span>
129 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
130 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> else</span>
131 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return fib(n-1) + fib(n-2);</span>
132 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
133 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
134 function( n ) ... end
135 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fib(15);</span>
136 610
137 </pre></div>
138
139 <p>There should be additional tests for the argument <code class="code">n</code> being a positive integer. This function <code class="code">fib</code> might lead to strange results if called with other arguments. Try inserting the necessary tests into this example.</p>
140
141 <p><a id="X797E24D67BB804A2" name="X797E24D67BB804A2"></a></p>
142
143 <h4>4.3 <span class="Heading">Local Variables</span></h4>
144
145 <p>A function <code class="code">gcd</code> that computes the greatest common divisor of two integers by Euclid's algorithm will need a variable in addition to the formal arguments.</p>
146
147
148 <div class="example"><pre>
149 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd:= function(a, b)</span>
150 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local c;</span>
151 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> while b &lt;&gt; 0 do</span>
152 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> c:= b;</span>
153 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> b:= a mod b;</span>
154 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> a:= c;</span>
155 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
156 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return c;</span>
157 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
158 function( a, b ) ... end
159 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd(30, 63);</span>
160 3
161 </pre></div>
162
163 <p>The additional variable <code class="code">c</code> is declared as a <em>local</em> variable in the <code class="keyw">local</code> statement of the function definition. The <code class="keyw">local</code> statement, if present, must be the first statement of a function definition. When several local variables are declared in only one <code class="keyw">local</code> statement they are separated by commas.</p>
164
165 <p>The variable <code class="code">c</code> is indeed a local variable, that is local to the function <code class="code">gcd</code>. If you try to use the value of <code class="code">c</code> in the main loop you will see that <code class="code">c</code> has no assigned value unless you have already assigned a value to the variable <code class="code">c</code> in the main loop. In this case the local nature of <code class="code">c</code> in the function <code class="code">gcd</code> prevents the value of the <code class="code">c</code> in the main loop from being overwritten.</p>
166
167
168 <div class="example"><pre>
169 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">c:= 7;;</span>
170 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd(30, 63);</span>
171 3
172 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">c;</span>
173 7
174 </pre></div>
175
176 <p>We say that in a given scope an identifier identifies a unique variable. A <em>scope</em> is a lexical part of a program text. There is the global scope 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 local scope introduces new variables, whose identifiers are given in the formal argument list and the local declaration of the function. The usage of an identifier in a program text refers to the variable in the innermost scope that has this identifier as its name.</p>
177
178 <p><a id="X7981E4197F7113EA" name="X7981E4197F7113EA"></a></p>
179
180 <h4>4.4 <span class="Heading">Recursion</span></h4>
181
182 <p>We have already seen recursion in the function <code class="code">fib</code> in Section <a href="chap4.html#X801EE07E839B31B2"><span class="RefLink">4.2</span></a>. Here is another, slightly more complicated example.</p>
183
184 <p>We will now write a function to determine the number of partitions of a positive integer. A partition of a positive integer is a descending list of numbers whose sum is the given integer. For example <span class="SimpleMath">[4,2,1,1]</span> is a partition of 8. Note that there is just one partition of 0, namely <span class="SimpleMath">[ ]</span>. The complete set of all partitions of an integer <span class="SimpleMath">n</span> may be divided into subsets with respect to the largest element. The number of partitions of <span class="SimpleMath">n</span> therefore equals the sum of the numbers of partitions of <span class="SimpleMath">n-i</span> with elements less than or equal to <span class="SimpleMath">i</span> for all possible <span class="SimpleMath">i</span>. More generally the number of partitions of <span class="SimpleMath">n</span> with elements less than <span class="SimpleMath">m</span> is the sum of the numbers of partitions of <span class="SimpleMath">n-i</span> with elements less than <span class="SimpleMath">i</span> for <span class="SimpleMath">i</span> less than <span class="SimpleMath">m</span> and <span class="SimpleMath">n</span>. This description yields the following function.</p>
185
186
187 <div class="example"><pre>
188 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">nrparts:= function(n)</span>
189 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local np;</span>
190 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> np:= function(n, m)</span>
191 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local i, res;</span>
192 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n = 0 then</span>
193 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
194 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
195 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> res:= 0;</span>
196 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> for i in [1..Minimum(n,m)] do</span>
197 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> res:= res + np(n-i, i);</span>
198 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
199 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return res;</span>
200 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
201 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return np(n,n);</span>
202 <span class="GAPprompt">&gt;</span> <span class="GAPinput">end;</span>
203 function( n ) ... end
204 </pre></div>
205
206 <p>We wanted to write a function that takes one argument. We solved the problem of determining the number of partitions in terms of a recursive procedure with two arguments. So we had to write in fact two functions. The function <code class="code">nrparts</code> that can be used to compute the number of partitions indeed takes only one argument. The function <code class="code">np</code> takes two arguments and solves the problem in the indicated way. The only task of the function <code class="code">nrparts</code> is to call <code class="code">np</code> with two equal arguments.</p>
207
208 <p>We made <code class="code">np</code> local to <code class="code">nrparts</code>. This illustrates the possibility of having local functions in <strong class="pkg">GAP</strong>. It is however not necessary to put it there. <code class="code">np</code> could as well be defined on the main level, but then the identifier <code class="code">np</code> would be bound and could not be used for other purposes, and if it were used the essential function <code class="code">np</code> would no longer be available for <code class="code">nrparts</code>.</p>
209
210 <p>Now have a look at the function <code class="code">np</code>. It has two local variables <code class="code">res</code> and <code class="code">i</code>. The variable <code class="code">res</code> is used to collect the sum and <code class="code">i</code> is a loop variable. In the loop the function <code class="code">np</code> calls itself again with other arguments. It would be very disturbing if this call of <code class="code">np</code> was to use the same <code class="code">i</code> and <code class="code">res</code> as the calling <code class="code">np</code>. Since the new call of <code class="code">np</code> creates a new scope with new variables this is fortunately not the case.</p>
211
212 <p>Note that the formal parameters <var class="Arg">n</var> and <var class="Arg">m</var> of <code class="code">np</code> are treated like local variables.</p>
213
214 <p>(Regardless of the recursive structure of an algorithm it is often cheaper (in terms of computing time) to avoid a recursive implementation if possible (and it is possible in this case), because a function call is not very cheap.)</p>
215
216 <p><a id="X85B6E21781804BBB" name="X85B6E21781804BBB"></a></p>
217
218 <h4>4.5 <span class="Heading">Further Information about Functions</span></h4>
219
220 <p>The function syntax is described in Section <span class="RefLink">Reference: Functions</span>. The <code class="keyw">if</code> statement is described in more detail in Section <span class="RefLink">Reference: If</span>. More about Fibonacci numbers is found in Section <code class="func">Fibonacci</code> (<span class="RefLink">Reference: Fibonacci</span>) and more about partitions in Section <code class="func">Partitions</code> (<span class="RefLink">Reference: Partitions</span>).</p>
221
222
223 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap3.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap5.html">[Next Chapter]</a>&nbsp; </div>
224
225
226 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
227
228 <hr />
229 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
230 </body>
231 </html>