1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<!-- %% -->
<!-- %W function.xml GAP documentation Alexander Hulpke -->
<!-- %% -->
<!-- %% -->
<!-- %Y Copyright 1999 -->
<!-- %% -->
<!-- %% This file describes functions for functions. -->
<!-- %% -->
<Chapter Label="Functions">
<Heading>Functions</Heading>
<Index Subkey="as in programming language">functions</Index>
The section <Ref Sect="Function"/> 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.
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Information about a function">
<Heading>Information about a function</Heading>
<#Include Label="NameFunction">
<#Include Label="NumberArgumentsFunction">
<#Include Label="NamesLocalVariablesFunction">
<#Include Label="FilenameFunc">
<#Include Label="StartlineFunc">
<#Include Label="LocationFunc">
<#Include Label="PageSource">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Calling a function with a list argument that is interpreted as
several arguments">
<Heading>Calling a function with a list argument that is interpreted as
several arguments</Heading>
<#Include Label="CallFuncList">
</Section>
<Section Label="Memoizing a function">
<Heading>Wrapping a function, so the values produced are cached</Heading>
<#Include Label="MemoizePosIntFunction">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Functions that do nothing">
<Heading>Functions that do nothing</Heading>
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 <Ref Func="ReturnTrue"/> is often used as family predicate in
<Ref Func="InstallMethod"/>.
<#Include Label="ReturnTrue">
<#Include Label="ReturnFalse">
<#Include Label="ReturnFail">
<#Include Label="ReturnNothing">
<#Include Label="ReturnFirst">
<#Include Label="IdFunc">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Function Types">
<Heading>Function Types</Heading>
Functions are &GAP; objects and thus have categories and a family.
<#Include Label="IsFunction">
<#Include Label="FunctionsFamily">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Naming Conventions">
<Heading>Naming Conventions</Heading>
The way functions are named in &GAP;
might help to memorize or even guess names of library functions.
<P/>
If a variable name consists of several words then the first
letter of each word is capitalized.
<P/>
If the first part of the name of a function is a verb then the function
may modify its argument(s) but does not return anything, for example
<Ref Oper="Append"/> appends the list given as second argument to the
list given as first argument.
Otherwise the function returns an object without changing the arguments,
for example <Ref Func="Concatenation" Label="for several lists"/>
returns the concatenation of the lists given as arguments.
<P/>
If the name of a function contains the word <Q><C>Of</C></Q> then the return
value is thought of as information deduced from the arguments.
Usually such functions are attributes (see <Ref Sect="Attributes"/>).
Examples are <Ref Attr="GeneratorsOfGroup"/>, which returns a list of
generators for the group entered as argument, or <Ref Func="DiagonalOfMat"/>.
<P/>
For the setter and tester functions of an attribute <C>Attr</C>
the names <C>SetAttr</C> resp. <C>HasAttr</C> are available
(see <Ref Sect="Attributes"/>).
<P/>
If the name of a function contains the word <Q><C>By</C></Q> then the
return value is thought of as built in a certain way from the parts
given as arguments. For example, creating a group as a factor group
of a given group by a normal subgroup can be done by taking the image
of <Ref Func="NaturalHomomorphismByNormalSubgroup"/>. Other examples of
<Q><C>By</C></Q> functions are <Ref Func="GroupHomomorphismByImages"/> and
<Ref Oper="LaurentPolynomialByCoefficients"/>.
<P/>
Often such functions construct an algebraic structure given by its generators
(for example, <Ref Oper="RingByGenerators"/>). In some cases, <Q><C>By</C></Q>
may be replaced by <Q><C>With</C></Q> (like e.g. <Ref Oper="GroupWithGenerators"/>)
or even both versions of the name may be used. The difference between
<C>StructByGenerators</C> and <C>StructWithGenerators</C> is that the latter
guarantees that the <C>GeneratorsOfStruct</C> value of the result is equal to
the given set of generators (see <Ref Label="Constructing Domains"/>).
<P/>
If the name of a function has the form <Q><C>AsSomething</C></Q> then the return
value is an object (usually a collection which has the same family of elements),
which may, for example:
<List>
<Item>
know more about its own structure (and so support more operations) than its
input (e.g. if the elements of the collection form a group,
then this group can be constructed using <Ref Attr="AsGroup"/>);
</Item>
<Item>
discard its additional structure (e.g. <Ref Attr="AsList"/> applied to a
group will return a list of its elements);
</Item>
<Item>
contain all elements of the original object without duplicates
(like e.g. <Ref Attr="AsSet"/> does if its argument is a list of elements
from the same family);
</Item>
<Item>
remain unchanged (like e.g. <Ref Oper="AsSemigroup"/> does if
its argument is a group).
</Item>
</List>
If <C>Something</C> and the argument of <C>AsSomething</C> are domains,
some further rules apply as explained in
<Ref Label="Changing the Structure" BookName="tut"/>.
<P/>
If the name of a function <C>fun1</C> ends with <Q><C>NC</C></Q> then there
is another function <C>fun2</C> with the same name except that the <C>NC</C>
is missing. <C>NC</C> stands for <Q>no check</Q>. When <C>fun2</C> is called
then it checks whether its arguments are valid, and if so then it calls
<C>fun1</C>. The functions <Ref Func="SubgroupNC"/> and
<Ref Func="Subgroup"/> are a typical example.
<P/>
The idea is that the possibly time consuming check of the arguments
can be omitted if one is sure that they are unnecessary.
For example, if an algorithm produces generators of the derived subgroup
of a group then it is guaranteed that they lie in the original group;
<Ref Func="Subgroup"/> would check this, and <Ref Func="SubgroupNC"/>
omits the check.
<P/>
Needless to say, all these rules are not followed slavishly, for example
there is one operation <Ref Attr="Zero"/> instead of two operations
<C>ZeroOfElement</C> and <C>ZeroOfAdditiveGroup</C>.
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Pragmas">
<Heading>Code annotations (pragmas)</Heading>
<Index>Pragmas</Index>
<Index>Code annotations</Index>
<Index>#%</Index>
GAP supports the use of code annotations (pragmas) in functions, i.e.,
adding comments to functions that are stored in the function object itself,
unlike regular comments. Pragmas are single-line comments,
starting with <C>#%</C>:
<Example><![CDATA[
gap> function()
> #% This is a pragma
> # This is not a pragma
> return;
> end;;
gap> Display( last );
function ( )
#% This is a pragma
return;
end
]]></Example>
<P/>
Pragmas can be used to mark parts of functions that should
later be manipulated using <Ref Sect="Syntax Trees"/>.
<P/>
Please note that heavy use of pragmas in functions slows
down the execution of your function in the same way as adding
empty <C>;</C> statements to your code.
<Log><![CDATA[
gap> a := function( )
> local i;
> for i in [ 1 .. 1000000 ] do
> i := i + 1;
> od;
> end;
function( ) ... end
gap> a();
gap> time;
14
gap> b := function( )
> local i;
> for i in [ 1 .. 1000000 ] do
> i := i + 1;
> #% pragma
> #% pragma
> #% pragma
> #% pragma
> #% pragma
> od;
> end;
function( ) ... end
gap> b();
gap> time;
25
]]></Log>
</Section>
</Chapter>
|