Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 0c1f9463f03451b5503f0c33beb88a98 > files > 147

gap-system-4.4.12-5mdv2010.0.x86_64.rpm

<html><head><title>[ref] 5 Functions</title></head>
<body text="#000000" bgcolor="#ffffff">
[<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP004.htm">Previous</a>] [<a href ="CHAP006.htm">Next</a>] [<a href = "theindex.htm">Index</a>]
<h1>5 Functions</h1><p>
<P>
<H3>Sections</H3>
<oL>
<li> <A HREF="CHAP005.htm#SECT001">Information about a function</a>
<li> <A HREF="CHAP005.htm#SECT002">Calling a function with a list argument that is interpreted as several arguments</a>
<li> <A HREF="CHAP005.htm#SECT003">Functions that do nothing</a>
<li> <A HREF="CHAP005.htm#SECT004">Function Types</a>
</ol><p>
<p>
<a name = "I0"></a>

The section&nbsp;<a href="CHAP004.htm#SSEC022.1">Function</a> describes how to define a function. In this chapter
we describe functions that give information about functions, and various
utility functions used either when defining functions or calling functions.
<p>
<p>
<h2><a name="SECT001">5.1 Information about a function</a></h2>
<p><p>
<a name = "SSEC001.1"></a>
<li><code>NameFunction( </code><var>func</var><code> ) F</code>
<p>
returns the name of a function. For operations, this is the name used in
their declaration. For functions, this is the variable name they were
first assigned to. (For some internal functions, this might be a name
<strong>different</strong> from the name that is documented.)
If no such name exists, <code>"unknown"</code> is returned.
<p>
<pre>
gap&gt; NameFunction(SylowSubgroup);
"SylowSubgroup"
gap&gt; Blubberflutsch:=x-&gt;x;;
gap&gt; NameFunction(Blubberflutsch);
"Blubberflutsch"
gap&gt; a:=Blubberflutsch;;
gap&gt; NameFunction(a);
"Blubberflutsch"
gap&gt; NameFunction(x-&gt;x);
"unknown"
gap&gt; NameFunction(NameFunction);
"NAME_FUNC"
</pre>
<p>
<a name = "SSEC001.2"></a>
<li><code>NumberArgumentsFunction( </code><var>func</var><code> ) F</code>
<p>
returns the number of arguments the function <var>func</var> accepts. For
functions that use <code>arg</code> to take a variable number of arguments, as well
as for operations, -1 is returned. For attributes, 1 is returned.
<p>
<pre>
gap&gt; NumberArgumentsFunction(function(a,b,c,d,e,f,g,h,i,j,k)return 1;end);
11
gap&gt; NumberArgumentsFunction(Size);
1
gap&gt; NumberArgumentsFunction(IsCollsCollsElms);
3
gap&gt; NumberArgumentsFunction(Sum);
-1
</pre>
<p>
<a name = "SSEC001.3"></a>
<li><code>NamesLocalVariablesFunction( </code><var>func</var><code> ) F</code>
<p>
returns a mutable list of strings;
the first entries are the names of the arguments of the function <var>func</var>,
in the same order as they were entered in the definition of <var>func</var>,
and the remaining ones are the local variables as given in the <code>local</code>
statement in <var>func</var>.
(The number of arguments can be computed with <code>NumberArgumentsFunction</code>.)
<p>
<pre>
gap&gt; NamesLocalVariablesFunction( function( a, b ) local c; return 1; end );
[ "a", "b", "c" ]
gap&gt; NamesLocalVariablesFunction( function( arg ) local a; return 1; end );
[ "arg", "a" ]
gap&gt; NamesLocalVariablesFunction( Size );
fail
</pre>
<p>
<p>
<h2><a name="SECT002">5.2 Calling a function with a list argument that is interpreted as several arguments</a></h2>
<p><p>
<a name = "SSEC002.1"></a>
<li><code>CallFuncList( </code><var>func</var><code>, </code><var>args</var><code> ) F</code>
<p>
returns the result, when calling function <var>func</var> with the arguments
given in the list <var>args</var>, i.e.&nbsp;<var>args</var> is ``unwrapped'' so that <var>args</var> 
appears as several arguments to <var>func</var>.
<p>
<pre>
gap&gt; CallFuncList(\+, [6, 7]);
13
gap&gt; #is equivalent to:
gap&gt; \+(6, 7);
13
</pre>
<p>
A more useful application of <code>CallFuncList</code> is for a function <var>g</var> that is
called in the body of a function <var>f</var> with (a sublist of) the arguments of
<var>f</var>, where <var>f</var> has been defined  with  a  single  formal  argument  <code>arg</code>
(see&nbsp;<a href="CHAP004.htm#SSEC022.1">function</a>); see the following code fragment.
<p>
<pre>
f := function ( arg )
       CallFuncList(g, arg);
       ...
     end;
</pre>
<p>
In the body of <var>f</var> the several arguments passed  to  <var>f</var>  become  a  list
<code>arg</code>. If <var>g</var> were called instead via <code></code><var>g</var><code>( arg )</code> then <var>g</var> would  see  a
single list argument, so that <var>g</var> would, in general, have  to  ``unwrap''
the  passed  list.  The  following  (not  particularly  useful)   example
demonstrates both described possibilities for the call to <var>g</var>.
<p>
<pre>
gap&gt; PrintNumberFromDigits := function ( arg )
&gt;     CallFuncList( Print, arg );
&gt;     Print( "\n" );
&gt;    end;
function( arg ) ... end
gap&gt; PrintNumberFromDigits( 1, 9, 7, 3, 2 );
19732
gap&gt; PrintDigits := function ( arg )
&gt;     Print( arg );
&gt;     Print( "\n" );
&gt;    end;
function( arg ) ... end
gap&gt; PrintDigits( 1, 9, 7, 3, 2 );
[ 1, 9, 7, 3, 2 ]
</pre>
<p>
<p>
<h2><a name="SECT003">5.3 Functions that do nothing</a></h2>
<p><p>
The following functions return fixed results (or just their own argument).
They can be useful in places when the syntax requires a function, but
actually no functionality is required. So <code>ReturnTrue</code> is often used as
family predicate in <code>InstallMethod</code>
(see&nbsp;<a href="../prg/CHAP002.htm#SSEC002.1">InstallMethod</a> in ``Programming in <font face="Gill Sans,Helvetica,Arial">GAP</font>'').
<p>
<a name = "SSEC003.1"></a>
<li><code>ReturnTrue( ... ) F</code>
<p>
This function takes any number of arguments, and always returns <code>true</code>.
<p>
<a name = "SSEC003.2"></a>
<li><code>ReturnFalse( ... ) F</code>
<p>
This function takes any number of arguments, and always returns <code>false</code>.
<p>
<a name = "SSEC003.3"></a>
<li><code>ReturnFail( ... ) F</code>
<p>
This function takes any number of arguments, and always returns <code>fail</code>.
<p>
<a name = "SSEC003.4"></a>
<li><code>IdFunc( </code><var>obj</var><code> ) F</code>
<p>
returns <var>obj</var>.
<p>
<p>
<h2><a name="SECT004">5.4 Function Types</a></h2>
<p><p>
Functions are <font face="Gill Sans,Helvetica,Arial">GAP</font> objects and thus have categories and a family.
<p>
<a name = "SSEC004.1"></a>
<li><code>IsFunction( </code><var>obj</var><code> ) C</code>
<p>
is the category of functions.
<p>
<a name = "SSEC004.2"></a>
<li><code>IsOperation( </code><var>obj</var><code> ) C</code>
<p>
is the category of operations. Every operation is a function, but not
vice versa.
<p>
<a name = "SSEC004.3"></a>
<li><code>FunctionsFamily V</code>
<p>
is the family of all functions.
<p>
<p>
[<a href="../index.htm">Top</a>] [<a href = "chapters.htm">Up</a>] [<a href ="CHAP004.htm">Previous</a>] [<a href ="CHAP006.htm">Next</a>] [<a href = "theindex.htm">Index</a>]
<P>
<font face="Gill Sans,Helvetica,Arial">GAP 4 manual<br>December 2008
</font></body></html>