Debian Codegraph @

doc/tut/chap4_mj.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 <script type="text/javascript"
9 src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
10 </script>
11 <title>GAP (tut) - Chapter 4: Functions</title>
12 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
13 <meta name="generator" content="GAPDoc2HTML" />
14 <link rel="stylesheet" type="text/css" href="manual.css" />
15 <script src="manual.js" type="text/javascript"></script>
16 <script type="text/javascript">overwriteStyle();</script>
17 </head>
18 <body class="chap4" onload="jscontent()">
19
20
21 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chapBib_mj.html">Bib</a> <a href="chapInd_mj.html">Ind</a> </div>
22
23 <div class="chlinkprevnexttop">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap3_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap5_mj.html">[Next Chapter]</a>&nbsp; </div>
24
25 <p id="mathjaxlink" class="pcenter"><a href="chap4.html">[MathJax off]</a></p>
26 <p><a id="X86FA580F8055B274" name="X86FA580F8055B274"></a></p>
27 <div class="ChapSects"><a href="chap4_mj.html#X86FA580F8055B274">4 <span class="Heading">Functions</span></a>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4_mj.html#X7D81680E7BB09F45">4.1 <span class="Heading">Writing Functions</span></a>
29 </span>
30 </div>
31 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4_mj.html#X801EE07E839B31B2">4.2 <span class="Heading">If Statements</span></a>
32 </span>
33 </div>
34 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4_mj.html#X797E24D67BB804A2">4.3 <span class="Heading">Local Variables</span></a>
35 </span>
36 </div>
37 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4_mj.html#X7981E4197F7113EA">4.4 <span class="Heading">Recursion</span></a>
38 </span>
39 </div>
40 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap4_mj.html#X85B6E21781804BBB">4.5 <span class="Heading">Further Information about Functions</span></a>
41 </span>
42 </div>
43 </div>
44
45 <h3>4 <span class="Heading">Functions</span></h3>
46
47 <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>
48
49 <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>
50
51 <p><a id="X7D81680E7BB09F45" name="X7D81680E7BB09F45"></a></p>
52
53 <h4>4.1 <span class="Heading">Writing Functions</span></h4>
54
55 <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>
56
57
58 <div class="example"><pre>
59 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sayhello:= function()</span>
60 <span class="GAPprompt">&gt;</span> <span class="GAPinput">Print("hello, world.\n");</span>
61 <span class="GAPprompt">&gt;</span> <span class="GAPinput">end;</span>
62 function( ) ... end
63 </pre></div>
64
65 <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>
66
67 <p>The function definition has the following syntax.</p>
68
69 <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>
70
71 <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>
72
73 <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>
74
75
76 <div class="example"><pre>
77 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Print(sayhello, "\n");</span>
78 function ( )
79 Print( "hello, world.\n" );
80 return;
81 end
82 </pre></div>
83
84 <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>
85
86 <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>
87
88
89 <div class="example"><pre>
90 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sayhello();</span>
91 hello, world.
92 </pre></div>
93
94 <p>However, this is not a typical example as no value is returned but only a string is printed.</p>
95
96 <p><a id="X801EE07E839B31B2" name="X801EE07E839B31B2"></a></p>
97
98 <h4>4.2 <span class="Heading">If Statements</span></h4>
99
100 <p>In the following example we define a function <code class="code">sign</code> which determines the sign of an integer.</p>
101
102
103 <div class="example"><pre>
104 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sign:= function(n)</span>
105 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n &lt; 0 then</span>
106 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return -1;</span>
107 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> elif n = 0 then</span>
108 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 0;</span>
109 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> else</span>
110 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
111 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
112 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
113 function( n ) ... end
114 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sign(0); sign(-99); sign(11);</span>
115 0
116 -1
117 1
118 </pre></div>
119
120 <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>
121
122 <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>
123
124 <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>
125
126 <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 \geq 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>
127
128
129 <div class="example"><pre>
130 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fib:= function(n)</span>
131 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n in [1, 2] then</span>
132 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
133 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> else</span>
134 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return fib(n-1) + fib(n-2);</span>
135 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
136 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
137 function( n ) ... end
138 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fib(15);</span>
139 610
140 </pre></div>
141
142 <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>
143
144 <p><a id="X797E24D67BB804A2" name="X797E24D67BB804A2"></a></p>
145
146 <h4>4.3 <span class="Heading">Local Variables</span></h4>
147
148 <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>
149
150
151 <div class="example"><pre>
152 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd:= function(a, b)</span>
153 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local c;</span>
154 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> while b &lt;&gt; 0 do</span>
155 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> c:= b;</span>
156 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> b:= a mod b;</span>
157 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> a:= c;</span>
158 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
159 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return c;</span>
160 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
161 function( a, b ) ... end
162 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd(30, 63);</span>
163 3
164 </pre></div>
165
166 <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>
167
168 <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>
169
170
171 <div class="example"><pre>
172 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">c:= 7;;</span>
173 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">gcd(30, 63);</span>
174 3
175 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">c;</span>
176 7
177 </pre></div>
178
179 <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>
180
181 <p><a id="X7981E4197F7113EA" name="X7981E4197F7113EA"></a></p>
182
183 <h4>4.4 <span class="Heading">Recursion</span></h4>
184
185 <p>We have already seen recursion in the function <code class="code">fib</code> in Section <a href="chap4_mj.html#X801EE07E839B31B2"><span class="RefLink">4.2</span></a>. Here is another, slightly more complicated example.</p>
186
187 <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>
188
189
190 <div class="example"><pre>
191 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">nrparts:= function(n)</span>
192 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local np;</span>
193 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> np:= function(n, m)</span>
194 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> local i, res;</span>
195 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if n = 0 then</span>
196 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return 1;</span>
197 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
198 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> res:= 0;</span>
199 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> for i in [1..Minimum(n,m)] do</span>
200 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> res:= res + np(n-i, i);</span>
201 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
202 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return res;</span>
203 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end;</span>
204 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return np(n,n);</span>
205 <span class="GAPprompt">&gt;</span> <span class="GAPinput">end;</span>
206 function( n ) ... end
207 </pre></div>
208
209 <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>
210
211 <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>
212
213 <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>
214
215 <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>
216
217 <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>
218
219 <p><a id="X85B6E21781804BBB" name="X85B6E21781804BBB"></a></p>
220
221 <h4>4.5 <span class="Heading">Further Information about Functions</span></h4>
222
223 <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>
224
225
226 <div class="chlinkprevnextbot">&nbsp;<a href="chap0_mj.html">[Top of Book]</a>&nbsp; <a href="chap0_mj.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap3_mj.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap5_mj.html">[Next Chapter]</a>&nbsp; </div>
227
228
229 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0_mj.html">Top</a> <a href="chap1_mj.html">1</a> <a href="chap2_mj.html">2</a> <a href="chap3_mj.html">3</a> <a href="chap4_mj.html">4</a> <a href="chap5_mj.html">5</a> <a href="chap6_mj.html">6</a> <a href="chap7_mj.html">7</a> <a href="chap8_mj.html">8</a> <a href="chapBib_mj.html">Bib</a> <a href="chapInd_mj.html">Ind</a> </div>
230
231 <hr />
232 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
233 </body>
234 </html>