Debian Codegraph @

doc/ref/chap21.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 (ref) - Chapter 21: Lists</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="chap21" 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="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</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="chap20.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap22.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap21_mj.html">[MathJax on]</a></p>
23 <p><a id="X7B256AE5780F140A" name="X7B256AE5780F140A"></a></p>
24 <div class="ChapSects"><a href="chap21.html#X7B256AE5780F140A">21 <span class="Heading">Lists</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X86B28F5B781FFD31">21.1 <span class="Heading">List Categories</span></a>
26 </span>
27 <div class="ContSSBlock">
28 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C4CC4EA8299701E">21.1-1 IsList</a></span>
29 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X870AA9D8798C93DD">21.1-2 IsDenseList</a></span>
30 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C71596C82B6EF35">21.1-3 IsHomogeneousList</a></span>
31 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X80872FAF80EB5DF9">21.1-4 IsTable</a></span>
32 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79581E0387F7F7A9">21.1-5 IsRectangularTable</a></span>
33 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C84E16A85C99C8C">21.1-6 IsConstantTimeAccessList</a></span>
34 </div></div>
35 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7B202D147A5C2884">21.2 <span class="Heading">Basic Operations for Lists</span></a>
36 </span>
37 <div class="ContSSBlock">
38 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8297BBCD79642BE6"><code>21.2-1 <span>\</span>[<span>\</span>]</code></a></span>
39 </div></div>
40 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7921047F83F5FA28">21.3 <span class="Heading">List Elements</span></a>
41 </span>
42 <div class="ContSSBlock">
43 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X78791B8B838A8BA0"><code>21.3-1 \{\}</code></a></span>
44 </div></div>
45 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X8611EF768210625B">21.4 <span class="Heading">List Assignment</span></a>
46 </span>
47 <div class="ContSSBlock">
48 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X813FF1637F8D2B7F"><code>21.4-1 \{\}\:\=</code></a></span>
49 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X795EC9D67E34DAB0">21.4-2 Add</a></span>
50 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E98B11B79BA9167">21.4-3 Remove</a></span>
51 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79D7E96F80A2D7C0">21.4-4 CopyListEntries</a></span>
52 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79E31DB27C82D6E1">21.4-5 Append</a></span>
53 </div></div>
54 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7963C8E17EFF86DB">21.5 <span class="Heading">IsBound and Unbind for Lists</span></a>
55 </span>
56 <div class="ContSSBlock">
57 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79EC565A7DCEC938">21.5-1 IsBound</a></span>
58 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X866F45D3797FDA00">21.5-2 GetWithDefault</a></span>
59 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X78B72FDF7BD63C0B">21.5-3 Unbind</a></span>
60 </div></div>
61 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7DD65BEA7EDB0CD7">21.6 <span class="Heading">Identical Lists</span></a>
62 </span>
63 </div>
64 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7ED7C0738495556F">21.7 <span class="Heading">Duplication of Lists</span></a>
65 </span>
66 </div>
67 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X808A207182B2F84F">21.8 <span class="Heading">Membership Test for Lists</span></a>
68 </span>
69 <div class="ContSSBlock">
70 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B914A287F88ED0A"><code>21.8-1 \in</code></a></span>
71 </div></div>
72 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X84D6FC7E7E39ED33">21.9 <span class="Heading">Enlarging Internally Represented Lists</span></a>
73 </span>
74 <div class="ContSSBlock">
75 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X78BF67A5802E93AD">21.9-1 EmptyPlist</a></span>
76 </div></div>
77 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X8016D50F85147A77">21.10 <span class="Heading">Comparisons of Lists</span></a>
78 </span>
79 </div>
80 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X845EEAF083D43CCE">21.11 <span class="Heading">Arithmetic for Lists</span></a>
81 </span>
82 </div>
83 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X84D642967B8546B7">21.12 <span class="Heading">Filters Controlling the Arithmetic Behaviour of Lists</span></a>
84 </span>
85 <div class="ContSSBlock">
86 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X87ABCEE9809585A0">21.12-1 IsGeneralizedRowVector</a></span>
87 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FBCA5B58308C158">21.12-2 IsMultiplicativeGeneralizedRowVector</a></span>
88 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7BAD12E67BFC90DE">21.12-3 IsListDefault</a></span>
89 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8428E77B86722D52">21.12-4 NestingDepthA</a></span>
90 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X84B383B97FD986CD">21.12-5 NestingDepthM</a></span>
91 </div></div>
92 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7E6A1F66781BE923">21.13 <span class="Heading">Additive Arithmetic for Lists</span></a>
93 </span>
94 <div class="ContSSBlock">
95 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X86A85ADC85C451DC">21.13-1 <span class="Heading">Zero for lists</span></a>
96 </span>
97 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B91CE4D814C2D08">21.13-2 <span class="Heading">AdditiveInverse for lists</span></a>
98 </span>
99 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X842D123E7EE5E3DB">21.13-3 <span class="Heading">Addition of lists</span></a>
100 </span>
101 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C3DC8BE78DEECDE">21.13-4 <span class="Heading">Subtraction of lists</span></a>
102 </span>
103 </div></div>
104 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X782ED7F27D8C7FC1">21.14 <span class="Heading">Multiplicative Arithmetic for Lists</span></a>
105 </span>
106 <div class="ContSSBlock">
107 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79A8A5627FD42FA5">21.14-1 <span class="Heading">One for lists</span></a>
108 </span>
109 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X78C6C1E2849D303A">21.14-2 <span class="Heading">Inverse for lists</span></a>
110 </span>
111 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X84FDB95179BFE4CD">21.14-3 <span class="Heading">Multiplication of lists</span></a>
112 </span>
113 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82EA2A5B786181C7">21.14-4 <span class="Heading">Division of lists</span></a>
114 </span>
115 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7A0FD70C80B95C00">21.14-5 <span class="Heading">mod for lists</span></a>
116 </span>
117 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X84BB2DFB8432A1A4">21.14-6 <span class="Heading">Left quotients of lists</span></a>
118 </span>
119 </div></div>
120 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X8676EFE67972FD06">21.15 <span class="Heading">Mutability Status and List Arithmetic</span></a>
121 </span>
122 <div class="ContSSBlock">
123 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X80FDB1457FF582E7">21.15-1 ListWithIdenticalEntries</a></span>
124 </div></div>
125 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X8196FD4779BCCA0C">21.16 <span class="Heading">Finding Positions in Lists</span></a>
126 </span>
127 <div class="ContSSBlock">
128 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79975EC6783B4293">21.16-1 Position</a></span>
129 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FA9648883AE1B88">21.16-2 Positions</a></span>
130 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B4B10AE81602D4E">21.16-3 PositionCanonical</a></span>
131 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7D2B25B484591506">21.16-4 PositionNthOccurrence</a></span>
132 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7A122E848464E534">21.16-5 PositionSorted</a></span>
133 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X820BA44D85930EBF">21.16-6 PositionSortedBy</a></span>
134 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X78BFE9D78347C0DA">21.16-7 PositionSet</a></span>
135 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FD9C1D37F300206">21.16-8 PositionMaximum</a></span>
136 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E6C763A82C6153B">21.16-9 PositionProperty</a></span>
137 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7DA94D278304EC3D">21.16-10 PositionsProperty</a></span>
138 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X86C9E5C3863B3C03">21.16-11 PositionBound</a></span>
139 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X819F71047AABEA2F">21.16-12 PositionsBound</a></span>
140 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X865EF45D87ED1384">21.16-13 PositionNot</a></span>
141 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7F42E5AD87EC9D5A">21.16-14 PositionNonZero</a></span>
142 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X87A8C62A867D6DA4">21.16-15 PositionSublist</a></span>
143 </div></div>
144 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7865747A7CCF5812">21.17 <span class="Heading">Properties and Attributes for Lists</span></a>
145 </span>
146 <div class="ContSSBlock">
147 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X83F8EC7C7BF27EFC">21.17-1 IsMatchingSublist</a></span>
148 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FA892828252BB3B">21.17-2 IsDuplicateFree</a></span>
149 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7BAA9B0E81D4A884">21.17-3 IsSortedList</a></span>
150 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X80CDAF45782E8DCB">21.17-4 IsSSortedList</a></span>
151 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X780769238600AFD1">21.17-5 Length</a></span>
152 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B55FB967CDEF468">21.17-6 ConstantTimeAccessList</a></span>
153 </div></div>
154 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X83E558E37D1B44D4">21.18 <span class="Heading">Sorting Lists</span></a>
155 </span>
156 <div class="ContSSBlock">
157 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FE4975F8166884D">21.18-1 Sort</a></span>
158 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X791F2B2C7E9B9A46">21.18-2 SortParallel</a></span>
159 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X87287FCA81E2B06A">21.18-3 Sortex</a></span>
160 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X800209E881E7CECB">21.18-4 SortingPerm</a></span>
161 </div></div>
162 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X80ABC25582343910">21.19 <span class="Heading">Sorted Lists and Sets</span></a>
163 </span>
164 <div class="ContSSBlock">
165 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B16AD597CB12305"><code>21.19-1 \in</code></a></span>
166 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B4C0FEE7CDF6F2A">21.19-2 IsEqualSet</a></span>
167 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X79B940567A849216">21.19-3 IsSubsetSet</a></span>
168 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X832C23CC7FCD8892">21.19-4 AddSet</a></span>
169 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FCA282E789A4F4B">21.19-5 RemoveSet</a></span>
170 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B3469CD7EFC1A87">21.19-6 UniteSet</a></span>
171 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8473AA657FEC3D4D">21.19-7 IntersectSet</a></span>
172 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X80B427537EB07D09">21.19-8 SubtractSet</a></span>
173 </div></div>
174 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7DF510F7848CBBFD">21.20 <span class="Heading">Operations for Lists</span></a>
175 </span>
176 <div class="ContSSBlock">
177 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X840C55A77D1BB2E1">21.20-1 Concatenation</a></span>
178 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7CB0A6AF87C7FAF7">21.20-2 Compacted</a></span>
179 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7ECE9056792F28BA">21.20-3 Collected</a></span>
180 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8727F2928467C2F9">21.20-4 DuplicateFreeList</a></span>
181 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7F5D4DD87E4378AC">21.20-5 AsDuplicateFreeList</a></span>
182 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FA272D984EF82ED">21.20-6 Flat</a></span>
183 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C4FDB007C3F54A1">21.20-7 Reversed</a></span>
184 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8057372F83374193">21.20-8 Shuffle</a></span>
185 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8075FBDE7B81B4C8">21.20-9 Apply</a></span>
186 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7EF6E2BC81DBF6FB">21.20-10 Perform</a></span>
187 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8763882A7D65F979">21.20-11 PermListList</a></span>
188 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82CE0DE8828E4303">21.20-12 <span class="Heading">Maximum</span></a>
189 </span>
190 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82F133EC7F89665F">21.20-13 <span class="Heading">Minimum</span></a>
191 </span>
192 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X842851EB7E0969F7">21.20-14 <span class="Heading">MaximumList and MinimumList</span></a>
193 </span>
194 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E1593B979BDF2CD">21.20-15 <span class="Heading">Cartesian</span></a>
195 </span>
196 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E76F5A782184823">21.20-16 <span class="Heading">IteratorOfCartesianProduct</span></a>
197 </span>
198 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7B5A19098406347A">21.20-17 Permuted</a></span>
199 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X86CB7DCE8510F977">21.20-18 List</a></span>
200 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7C86D7F7795125F0">21.20-19 Filtered</a></span>
201 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8179B13D80E935FC">21.20-20 Number</a></span>
202 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82801DFA84E11272">21.20-21 First</a></span>
203 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E5B62E780421CE9">21.20-22 Last</a></span>
204 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7F06961278166671">21.20-23 ForAll</a></span>
205 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7AF82E747A8BDA75">21.20-24 ForAny</a></span>
206 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7E5C72F27B657948">21.20-25 Product</a></span>
207 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7A04B71C84CFCC2D">21.20-26 Sum</a></span>
208 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X834E4DF57F3A20F0">21.20-27 Iterated</a></span>
209 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7D150C2881881139">21.20-28 ListN</a></span>
210 </div></div>
211 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X805CA0B68029B47A">21.21 <span class="Heading">Advanced List Manipulations</span></a>
212 </span>
213 <div class="ContSSBlock">
214 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X799A66417CDC1D4F">21.21-1 FoldLeft</a></span>
215 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X83A2CF7A7F6C1DB9">21.21-2 FoldLeftX</a></span>
216 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8258477D7F72171B">21.21-3 ListX</a></span>
217 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7AC321B87A2DCAF5">21.21-4 SetX</a></span>
218 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82B1411E7FBE925F">21.21-5 SumX</a></span>
219 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7FB318B47D8783DA">21.21-6 ProductX</a></span>
220 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X86CF6FC87C5C9A08">21.21-7 ForAllX</a></span>
221 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X858C7CEF7EC1260C">21.21-8 ForAnyX</a></span>
222 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X877CF5DE7AE12506">21.21-9 FilteredX</a></span>
223 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7D6C138F84A3C985">21.21-10 NumberX</a></span>
224 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X82580BC887390C69">21.21-11 PerformX</a></span>
225 </div></div>
226 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X79596BDE7CAF8491">21.22 <span class="Heading">Ranges</span></a>
227 </span>
228 <div class="ContSSBlock">
229 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X86DDC2FF7A50FBEE">21.22-1 IsRange</a></span>
230 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X83896BC481536B07">21.22-2 IsRangeRep</a></span>
231 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7D22B2298167A58F">21.22-3 ConvertToRangeRep</a></span>
232 </div></div>
233 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X7EA3ACE27E43D174">21.23 <span class="Heading">Enumerators</span></a>
234 </span>
235 <div class="ContSSBlock">
236 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X7BB462C17962647F">21.23-1 IsQuickPositionList</a></span>
237 </div></div>
238 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap21.html#X81ECC2077D88E112">21.24 <span class="Heading">Plain Lists</span></a>
239 </span>
240 <div class="ContSSBlock">
241 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X8438CB908367254C">21.24-1 PlainListCopy</a></span>
242 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap21.html#X87BA4EBF80F16B72">21.24-2 IsPlistRep</a></span>
243 </div></div>
244 </div>
245
246 <h3>21 <span class="Heading">Lists</span></h3>
247
248 <p>Lists are the most important way to treat objects together. A <em>list</em> arranges objects in a definite order. So each list implies a partial mapping from the integers to the elements of the list. I.e., there is a first element of a list, a second, a third, and so on. Lists can occur in mutable or immutable form, see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a> for the concept of mutability, and <a href="chap21.html#X7ED7C0738495556F"><span class="RefLink">21.7</span></a> for the case of lists.</p>
249
250 <p>This chapter deals mainly with the aspect of lists in <strong class="pkg">GAP</strong> as <em>data structures</em>. Chapter <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a> tells more about the <em>collection</em> aspect of certain lists, and more about lists as <em>arithmetic objects</em> can be found in the chapters <a href="chap23.html#X82C7E6CF7BA03391"><span class="RefLink">23</span></a> and <a href="chap24.html#X812CCAB278643A59"><span class="RefLink">24</span></a>.</p>
251
252 <p>Lists are used to implement ranges (see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a>), sets (see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>), strings (see <a href="chap27.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>), row vectors and matrices (see <a href="chap23.html#X82C7E6CF7BA03391"><span class="RefLink">23</span></a> and <a href="chap24.html#X812CCAB278643A59"><span class="RefLink">24</span></a>, but note that <strong class="pkg">GAP</strong> supports also linear algebra for objects which are <em>not</em> lists, see <a href="chap26.html#X856C23B87E50F118"><span class="RefLink">26</span></a>); boolean lists (see <a href="chap22.html#X7AC531DD79B6938E"><span class="RefLink">22</span></a>) are a further special kind of lists.</p>
253
254 <p>Several operations for lists, such as <code class="func">Intersection</code> (<a href="chap30.html#X851069107CACF98E"><span class="RefLink">30.5-2</span></a>) and <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>), will be described in Chapter <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>, in particular see <a href="chap30.html#X7C3722DF8736FFDB"><span class="RefLink">30.3</span></a>.</p>
255
256 <p><a id="X86B28F5B781FFD31" name="X86B28F5B781FFD31"></a></p>
257
258 <h4>21.1 <span class="Heading">List Categories</span></h4>
259
260 <p>A list can be written by writing down the elements in order between square brackets <code class="code">[</code>, <code class="code">]</code>, and separating them with commas <code class="code">,</code>. An <em>empty list</em>, i.e., a list with no elements, is written as <code class="code">[]</code>.</p>
261
262
263 <div class="example"><pre>
264 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ]; # a list with three elements</span>
265 [ 1, 2, 3 ]
266 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ [], [ 1 ], [ 1, 2 ] ]; # a list may contain other lists</span>
267 [ [ ], [ 1 ], [ 1, 2 ] ]
268 </pre></div>
269
270 <p>Each list constructed this way is mutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>).</p>
271
272 <p><a id="X7C4CC4EA8299701E" name="X7C4CC4EA8299701E"></a></p>
273
274 <h5>21.1-1 IsList</h5>
275
276 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
277 <p>tests whether <var class="Arg">obj</var> is a list.</p>
278
279
280 <div class="example"><pre>
281 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsList( [ 1, 3, 5, 7 ] ); IsList( 1 );</span>
282 true
283 false
284 </pre></div>
285
286 <p><a id="X870AA9D8798C93DD" name="X870AA9D8798C93DD"></a></p>
287
288 <h5>21.1-2 IsDenseList</h5>
289
290 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsDenseList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
291 <p>A list is <em>dense</em> if it has no holes, i.e., contains an element at every position up to the length. It is absolutely legal to have lists with holes. They are created by leaving the entry between the commas empty. Holes at the end of a list are ignored. Lists with holes are sometimes convenient when the list represents a mapping from a finite, but not consecutive, subset of the positive integers.</p>
292
293
294 <div class="example"><pre>
295 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsDenseList( [ 1, 2, 3 ] );</span>
296 true
297 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ , 4, 9,, 25,, 49,,,, 121 ];; IsDenseList( l );</span>
298 false
299 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[3];</span>
300 9
301 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[4];</span>
302 List Element: &lt;list&gt;[4] must have an assigned value
303 not in any function
304 Entering break read-eval-print loop ...
305 you can 'quit;' to quit to outer loop, or
306 you can 'return;' after assigning a value to continue
307 <span class="GAPbrkprompt">brk&gt;</span> <span class="GAPinput">l[4] := 16;; # assigning a value</span>
308 <span class="GAPbrkprompt">brk&gt;</span> <span class="GAPinput">return; # to escape the break-loop</span>
309 16
310 gap&gt;
311 </pre></div>
312
313 <p>Observe that requesting the value of <code class="code">l[4]</code>, which was not assigned, caused the entry of a <code class="keyw">break</code>-loop (see Section <a href="chap6.html#X8593B49F8705B486"><span class="RefLink">6.4</span></a>). After assigning a value and typing <code class="code">return;</code>, <strong class="pkg">GAP</strong> is finally able to comply with our request (by responding with <code class="code">16</code>).</p>
314
315 <p><a id="X7C71596C82B6EF35" name="X7C71596C82B6EF35"></a></p>
316
317 <h5>21.1-3 IsHomogeneousList</h5>
318
319 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsHomogeneousList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
320 <p>returns <code class="keyw">true</code> if <var class="Arg">obj</var> is a list and it is homogeneous, and <code class="keyw">false</code> otherwise.</p>
321
322 <p>A <em>homogeneous</em> list is a dense list whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). The empty list is homogeneous but not a collection (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>), a nonempty homogeneous list is also a collection.</p>
323
324
325 <div class="example"><pre>
326 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsHomogeneousList( [ 1, 2, 3 ] ); IsHomogeneousList( [] );</span>
327 true
328 true
329 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsHomogeneousList( [ 1, false, () ] );</span>
330 false
331 </pre></div>
332
333 <p><a id="X80872FAF80EB5DF9" name="X80872FAF80EB5DF9"></a></p>
334
335 <h5>21.1-4 IsTable</h5>
336
337 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsTable</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
338 <p>A <em>table</em> is a nonempty list of homogeneous lists which lie in the same family. Typical examples of tables are matrices (see <a href="chap24.html#X812CCAB278643A59"><span class="RefLink">24</span></a>).</p>
339
340
341 <div class="example"><pre>
342 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsTable( [ [ 1, 2 ], [ 3, 4 ] ] ); # in fact a matrix</span>
343 true
344 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsTable( [ [ 1 ], [ 2, 3 ] ] ); # not rectangular but a table</span>
345 true
346 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsTable( [ [ 1, 2 ], [ () , (1,2) ] ] ); # not homogeneous</span>
347 false
348 </pre></div>
349
350 <p><a id="X79581E0387F7F7A9" name="X79581E0387F7F7A9"></a></p>
351
352 <h5>21.1-5 IsRectangularTable</h5>
353
354 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsRectangularTable</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
355 <p>A list lies in <code class="code">IsRectangularTable</code> when it is nonempty and its elements are all homogeneous lists of the same family and the same length.</p>
356
357 <p><a id="X7C84E16A85C99C8C" name="X7C84E16A85C99C8C"></a></p>
358
359 <h5>21.1-6 IsConstantTimeAccessList</h5>
360
361 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsConstantTimeAccessList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
362 <p>This category indicates whether the access to each element of the list <var class="Arg">list</var> will take roughly the same time. This is implied for example by <code class="code">IsList and IsInternalRep</code>, so all strings, Boolean lists, ranges, and internally represented plain lists are in this category.</p>
363
364 <p>But also other enumerators (see <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a>) can lie in this category if they guarantee constant time access to their elements.</p>
365
366 <p><a id="X7B202D147A5C2884" name="X7B202D147A5C2884"></a></p>
367
368 <h4>21.2 <span class="Heading">Basic Operations for Lists</span></h4>
369
370 <p>The basic operations for lists are element access (see <a href="chap21.html#X7921047F83F5FA28"><span class="RefLink">21.3</span></a>), assignment of elements to a list (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>), fetching the length of a list (see <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>)), the test for a hole at a given position, and unbinding an element at a given position (see <a href="chap21.html#X7963C8E17EFF86DB"><span class="RefLink">21.5</span></a>).</p>
371
372 <p>The term basic operation means that each other list operation can be formulated in terms of the basic operations. (But note that often a more efficient method than this one is implemented.)</p>
373
374 <p>Any <strong class="pkg">GAP</strong> object <var class="Arg">list</var> in the category <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>) is regarded as a list, and if methods for the basic list operations are installed for <var class="Arg">list</var> then <var class="Arg">list</var> can be used also for the other list operations.</p>
375
376 <p>For internally represented lists, kernel methods are provided for the basic list operations with positive integer indices. For other lists or other indices, it is possible to install appropriate methods for these operations. This permits the implementation of lists that do not need to store all list elements (see also <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a>); for example, the elements might be described by an algorithm, such as the elements list of a group. For this reduction of space requirements, however, a price in access time may have to be paid (see <code class="func">ConstantTimeAccessList</code> (<a href="chap21.html#X7B55FB967CDEF468"><span class="RefLink">21.17-6</span></a>)).</p>
377
378 <p><a id="X8297BBCD79642BE6" name="X8297BBCD79642BE6"></a></p>
379
380 <h5><code>21.2-1 <span>\</span>[<span>\</span>]</code></h5>
381
382 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; <span>\</span>[<span>\</span>]</code>( <var class="Arg">list</var>, <var class="Arg">ix</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
383 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsBound<span>\</span>[<span>\</span>]</code>( <var class="Arg">list</var>, <var class="Arg">ix</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
384 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; <span>\</span>[<span>\</span>]\:\=</code>( <var class="Arg">list</var>, <var class="Arg">pos</var>, <var class="Arg">ix</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
385 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Unbind<span>\</span>[<span>\</span>]</code>( <var class="Arg">list</var>, <var class="Arg">ix</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
386 <p>These operations implement element access, test for element boundedness, list element assignment, and removal of the element with index <var class="Arg">ix</var>.</p>
387
388 <p>Note that the special characters <code class="code">[</code>, <code class="code">]</code>, <code class="code">:</code>, and <code class="code">=</code> must be escaped with a backslash <code class="code">\</code> (see <a href="chap4.html#X7E90E6607F4E4943"><span class="RefLink">4.3</span></a>); so <code class="func"><span>\</span>[<span>\</span>]</code> denotes the operation for element access in a list, whereas <code class="code">[]</code> denotes an empty list. (Maybe the variable names involving special characters look strange, but nevertheless they are quite suggestive.)</p>
389
390 <p><code class="code">\[\]( <var class="Arg">list</var>, <var class="Arg">ix</var> )</code> is equivalent to <code class="code"><var class="Arg">list</var>[ <var class="Arg">ix</var> ]</code>, which clearly will usually be preferred; the former is useful mainly if one wants to access the operation itself, for example if one wants to install a method for element access in a special kind of lists.</p>
391
392 <p>Similarly, <code class="func">IsBound<span>\</span>[<span>\</span>]</code> is used explicitly mainly in method installations. In other situations, one can simply call <code class="func">IsBound</code> (<a href="chap21.html#X79EC565A7DCEC938"><span class="RefLink">21.5-1</span></a>), which then delegates to <code class="func">IsBound<span>\</span>[<span>\</span>]</code> if the first argument is a list, and to <code class="func">IsBound\.</code> (<a href="chap29.html#X7821AC097821AC09"><span class="RefLink">29.7-3</span></a>) if the first argument is a record.</p>
393
394 <p>Analogous statements hold for <code class="func"><span>\</span>[<span>\</span>]\:\=</code> and <code class="func">Unbind<span>\</span>[<span>\</span>]</code>.</p>
395
396 <p><a id="X7921047F83F5FA28" name="X7921047F83F5FA28"></a></p>
397
398 <h4>21.3 <span class="Heading">List Elements</span></h4>
399
400 <p><code class="code"><var class="Arg">list</var>[ <var class="Arg">ix</var> ]</code></p>
401
402 <p>The above construct evaluates to the element of the list <var class="Arg">list</var> with index <var class="Arg">ix</var>. For built-in list types and collections, indexing is done with origin 1, i.e., the first element of the list is the element with index 1.</p>
403
404
405 <div class="example"><pre>
406 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 2, 3, 5, 7, 11, 13 ];; l[1]; l[2]; l[6];</span>
407 2
408 3
409 13
410 </pre></div>
411
412 <p>If <var class="Arg">list</var> is not a built-in list, or <var class="Arg">ix</var> does not evaluate to a positive integer, method selection is invoked to try and find a way of indexing <var class="Arg">list</var> with index <var class="Arg">ix</var>. If this fails, or the selected method finds that <code class="code"><var class="Arg">list</var>[<var class="Arg">ix</var>]</code> is unbound, an error is signalled.</p>
413
414 <p><code class="code"><var class="Arg">list</var>{ <var class="Arg">poss</var> }</code></p>
415
416 <p>The above construct evaluates to a new list <var class="Arg">new</var> whose first element is <code class="code"><var class="Arg">list</var>[<var class="Arg">poss</var>[1]]</code>, whose second element is <code class="code"><var class="Arg">list</var>[<var class="Arg">poss</var>[2]]</code>, and so on. However, it does not need to be sorted and may contain duplicate elements. If for any <span class="SimpleMath">i</span>, <code class="code"><var class="Arg">list</var>[ <var class="Arg">poss</var>[</code><span class="SimpleMath">i</span><code class="code">] ]</code> is unbound, an error is signalled.</p>
417
418
419 <div class="example"><pre>
420 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 2, 3, 5, 7, 11, 13, 17, 19 ];;</span>
421 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l{[4..6]}; l{[1,7,1,8]};</span>
422 [ 7, 11, 13 ]
423 [ 2, 17, 2, 19 ]
424 </pre></div>
425
426 <p>The result is a <em>new</em> list, that is not identical to any other list. The elements of that list, however, are identical to the corresponding elements of the left operand (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>).</p>
427
428 <p>It is possible to nest such <em>sublist extractions</em>, as can be seen in the example below.</p>
429
430
431 <div class="example"><pre>
432 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m := [ [1,2,3], [4,5,6], [7,8,9], [10,11,12] ];; m{[1,2,3]}{[3,2]};</span>
433 [ [ 3, 2 ], [ 6, 5 ], [ 9, 8 ] ]
434 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := m{[1,2,3]};; l{[3,2]};</span>
435 [ [ 7, 8, 9 ], [ 4, 5, 6 ] ]
436 </pre></div>
437
438 <p>Note the difference between the two examples. The latter extracts elements 1, 2, and 3 from <var class="Arg">m</var> and then extracts the elements 3 and 2 from <em>this list</em>. The former extracts elements 1, 2, and 3 from <var class="Arg">m</var> and then extracts the elements 3 and 2 from <em>each of those element lists</em>.</p>
439
440 <p>To be precise: With each selector <code class="code">[<var class="Arg">pos</var>]</code> or <code class="code">{<var class="Arg">poss</var>}</code> we associate a <em>level</em> that is defined as the number of selectors of the form <code class="code">{<var class="Arg">poss</var>}</code> to its left in the same expression. For example</p>
441
442
443 <div class="example"><pre>
444 l[pos1]{poss2}{poss3}[pos4]{poss5}[pos6]
445 level 0 0 1 2 2 3
446 </pre></div>
447
448 <p>Then a selector <code class="code"><var class="Arg">list</var>[<var class="Arg">pos</var>]</code> of level <var class="Arg">level</var> is computed as <code class="code">ListElement(<var class="Arg">list</var>,<var class="Arg">pos</var>,<var class="Arg">level</var>)</code>, where <code class="code">ListElement</code> is defined as follows. (Note that <code class="code">ListElement</code> is <em>not</em> a <strong class="pkg">GAP</strong> function.)</p>
449
450
451 <div class="example"><pre>
452 ListElement := function ( list, pos, level )
453 if level = 0 then
454 return list[pos];
455 else
456 return List( list, elm -&gt; ListElement(elm,pos,level-1) );
457 fi;
458 end;
459 </pre></div>
460
461 <p>and a selector <code class="code"><var class="Arg">list</var>{<var class="Arg">poss</var>}</code> of level <var class="Arg">level</var> is computed as <code class="code">ListElements(<var class="Arg">list</var>,<var class="Arg">poss</var>,<var class="Arg">level</var>)</code>, where <code class="code">ListElements</code> is defined as follows. (Note that <code class="code">ListElements</code> is <em>not</em> a <strong class="pkg">GAP</strong> function.)</p>
462
463
464 <div class="example"><pre>
465 ListElements := function ( list, poss, level )
466 if level = 0 then
467 return list{poss};
468 else
469 return List( list, elm -&gt; ListElements(elm,poss,level-1) );
470 fi;
471 end;
472 </pre></div>
473
474 <p><a id="X78791B8B838A8BA0" name="X78791B8B838A8BA0"></a></p>
475
476 <h5><code>21.3-1 \{\}</code></h5>
477
478 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \{\}</code>( <var class="Arg">list</var>, <var class="Arg">poss</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
479 <p>This operation implements <em>sublist access</em>. For any list, the default method is to loop over the entries in the list <var class="Arg">poss</var>, and to delegate to the element access operation. (For nested sublist extractions, cf. <a href="chap21.html#X7921047F83F5FA28"><span class="RefLink">21.3</span></a>. For the somewhat strange variable name, cf. <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>.)</p>
480
481 <p><a id="X8611EF768210625B" name="X8611EF768210625B"></a></p>
482
483 <h4>21.4 <span class="Heading">List Assignment</span></h4>
484
485 <p><code class="code"><var class="Arg">list</var>[ <var class="Arg">ix</var> ] := <var class="Arg">object</var>;</code></p>
486
487 <p>The list element assignment assigns the object <var class="Arg">object</var>, which can be of any type, to the list with index <var class="Arg">ix</var>, in the mutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>) list <var class="Arg">list</var>. That means that accessing the <var class="Arg">ix</var>-th element of the list <var class="Arg">list</var> will return <var class="Arg">object</var> after this assignment.</p>
488
489
490 <div class="example"><pre>
491 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 1, 2, 3 ];;</span>
492 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[1] := 3;; l; # assign a new object</span>
493 [ 3, 2, 3 ]
494 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[2] := [ 4, 5, 6 ];; l; # &lt;object&gt; may be of any type</span>
495 [ 3, [ 4, 5, 6 ], 3 ]
496 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[ l[1] ] := 10;; l; # &lt;index&gt; may be an expression</span>
497 [ 3, [ 4, 5, 6 ], 10 ]
498 </pre></div>
499
500 <p>If the index <var class="Arg">ix</var> is an integer larger than the length of the list <var class="Arg">list</var> (see <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>)), the list is automatically enlarged to make room for the new element. Note that it is possible to generate lists with holes that way.</p>
501
502
503 <div class="example"><pre>
504 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[4] := "another entry";; l; # &lt;list&gt; is enlarged</span>
505 [ 3, [ 4, 5, 6 ], 10, "another entry" ]
506 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[ 10 ] := 1;; l; # now &lt;list&gt; has a hole</span>
507 [ 3, [ 4, 5, 6 ], 10, "another entry",,,,,, 1 ]
508 </pre></div>
509
510 <p>The function <code class="func">Add</code> (<a href="chap21.html#X795EC9D67E34DAB0"><span class="RefLink">21.4-2</span></a>) should be used if you want to add an element to the end of the list.</p>
511
512 <p>Note that assigning to a list changes the list, thus this list must be mutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>). See <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a> for subtleties of changing lists.</p>
513
514 <p>If <var class="Arg">list</var> does not evaluate to a list, <var class="Arg">pos</var> does not evaluate to a positive integer, method selection is invoked to try and find a way of indexing <var class="Arg">list</var> with index <var class="Arg">pos</var>. If this fails, or the selected method finds that <code class="code"><var class="Arg">list</var>[<var class="Arg">pos</var>]</code> is unbound, or if <var class="Arg">object</var> is a call to a function which does not return a value (for example <code class="code">Print</code>) an error is signalled.</p>
515
516 <p><code class="code"><var class="Arg">list</var>{ <var class="Arg">poss</var> } := <var class="Arg">objects</var>;</code></p>
517
518 <p>The sublist assignment assigns the object <code class="code"><var class="Arg">objects</var>[1]</code>, which can be of any type, to the list <var class="Arg">list</var> at the position <code class="code"><var class="Arg">poss</var>[1]</code>, the object <code class="code"><var class="Arg">objects</var>[2]</code> to <code class="code"><var class="Arg">list</var>[<var class="Arg">poss</var>[2]]</code>, and so on. <var class="Arg">poss</var> must be a dense list of positive integers, it need, however, not be sorted and may contain duplicate elements. <var class="Arg">objects</var> must be a dense list and must have the same length as <var class="Arg">poss</var>.</p>
519
520
521 <div class="example"><pre>
522 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 2, 3, 5, 7, 11, 13, 17, 19 ];;</span>
523 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l{[1..4]} := [10..13];; l;</span>
524 [ 10, 11, 12, 13, 11, 13, 17, 19 ]
525 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l{[1,7,1,10]} := [ 1, 2, 3, 4 ];; l;</span>
526 [ 3, 11, 12, 13, 11, 13, 2, 19,, 4 ]
527 </pre></div>
528
529 <p>The next example shows that it is possible to nest such sublist assignments.</p>
530
531
532 <div class="example"><pre>
533 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m := [ [1,2,3], [4,5,6], [7,8,9], [10,11,12] ];;</span>
534 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m{[1,2,3]}{[3,2]} := [ [11,12], [13,14], [15,16] ];; m;</span>
535 [ [ 1, 12, 11 ], [ 4, 14, 13 ], [ 7, 16, 15 ], [ 10, 11, 12 ] ]
536 </pre></div>
537
538 <p>The exact behaviour is defined in the same way as for list extractions (see <a href="chap21.html#X7921047F83F5FA28"><span class="RefLink">21.3</span></a>). Namely, with each selector <code class="code">[<var class="Arg">pos</var>]</code> or <code class="code">{<var class="Arg">poss</var>}</code> we associate a <em>level</em> that is defined as the number of selectors of the form <code class="code">{<var class="Arg">poss</var>}</code> to its left in the same expression. For example</p>
539
540
541 <div class="example"><pre>
542 l[pos1]{poss2}{poss3}[pos4]{poss5}[pos6]
543 level 0 0 1 1 1 2
544 </pre></div>
545
546 <p>Then a list assignment <code class="code"><var class="Arg">list</var>[<var class="Arg">pos</var>] := <var class="Arg">vals</var>;</code> of level <var class="Arg">level</var> is computed as <code class="code">ListAssignment( <var class="Arg">list</var>, <var class="Arg">pos</var>, <var class="Arg">vals</var>, <var class="Arg">level</var> )</code>, where <code class="code">ListAssignment</code> is defined as follows. (Note that <code class="code">ListAssignment</code> is <em>not</em> a <strong class="pkg">GAP</strong> function.)</p>
547
548
549 <div class="example"><pre>
550 ListAssignment := function ( list, pos, vals, level )
551 local i;
552 if level = 0 then
553 list[pos] := vals;
554 else
555 for i in [1..Length(list)] do
556 ListAssignment( list[i], pos, vals[i], level-1 );
557 od;
558 fi;
559 end;
560 </pre></div>
561
562 <p>and a list assignment <code class="code"><var class="Arg">list</var>{<var class="Arg">poss</var>} := <var class="Arg">vals</var></code> of level <var class="Arg">level</var> is computed as <code class="code">ListAssignments( <var class="Arg">list</var>, <var class="Arg">poss</var>, <var class="Arg">vals</var>, <var class="Arg">level</var> )</code>, where <code class="code">ListAssignments</code> is defined as follows. (Note that <code class="code">ListAssignments</code> is <em>not</em> a <strong class="pkg">GAP</strong> function.)</p>
563
564
565 <div class="example"><pre>
566 ListAssignments := function ( list, poss, vals, level )
567 local i;
568 if level = 0 then
569 list{poss} := vals;
570 else
571 for i in [1..Length(list)] do
572 ListAssignments( list[i], poss, vals[i], level-1 );
573 od;
574 fi;
575 end;
576 </pre></div>
577
578 <p><a id="X813FF1637F8D2B7F" name="X813FF1637F8D2B7F"></a></p>
579
580 <h5><code>21.4-1 \{\}\:\=</code></h5>
581
582 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \{\}\:\=</code>( <var class="Arg">list</var>, <var class="Arg">poss</var>, <var class="Arg">val</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
583 <p>This operation implements sublist assignment. For any list, the default method is to loop over the entries in the list <var class="Arg">poss</var>, and to delegate to the element assignment operation. (For the somewhat strange variable name, cf. <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>.)</p>
584
585 <p><a id="X795EC9D67E34DAB0" name="X795EC9D67E34DAB0"></a></p>
586
587 <h5>21.4-2 Add</h5>
588
589 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Add</code>( <var class="Arg">list</var>, <var class="Arg">obj</var>[, <var class="Arg">pos</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
590 <p>adds the element <var class="Arg">obj</var> to the mutable list <var class="Arg">list</var>. The two argument version adds <var class="Arg">obj</var> at the end of <var class="Arg">list</var>, i.e., it is equivalent to the assignment <code class="code"><var class="Arg">list</var>[ Length(<var class="Arg">list</var>) + 1 ] := <var class="Arg">obj</var></code>, see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>.</p>
591
592 <p>The three argument version adds <var class="Arg">obj</var> in position <var class="Arg">pos</var>, moving all later elements of the list (if any) up by one position. Any holes at or after position <var class="Arg">pos</var> are also moved up by one position, and new holes are created before <var class="Arg">pos</var> if they are needed.</p>
593
594 <p>Nothing is returned by <code class="func">Add</code>, the function is only called for its side effect.</p>
595
596 <p><a id="X7E98B11B79BA9167" name="X7E98B11B79BA9167"></a></p>
597
598 <h5>21.4-3 Remove</h5>
599
600 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Remove</code>( <var class="Arg">list</var>[, <var class="Arg">pos</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
601 <p>removes an element from <var class="Arg">list</var>. The one argument form removes the last element. The two argument form removes the element in position <var class="Arg">pos</var>, moving all subsequent elements down one position. Any holes after position <var class="Arg">pos</var> are also moved down by one position.</p>
602
603 <p>The one argument form always returns the removed element. In this case <var class="Arg">list</var> must be non-empty.</p>
604
605 <p>The two argument form returns the old value of <var class="Arg">list</var>[<var class="Arg">pos</var>] if it was bound, and nothing if it was not. Note that accessing or assigning the return value of this form of the <code class="func">Remove</code> operation is only safe when you <em>know</em> that there will be a value, otherwise it will cause an error.</p>
606
607
608 <div class="example"><pre>
609 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 2, 3, 5 ];; Add( l, 7 ); l;</span>
610 [ 2, 3, 5, 7 ]
611 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Add(l,4,2); l;</span>
612 [ 2, 4, 3, 5, 7 ]
613 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Remove(l,2); l;</span>
614 4
615 [ 2, 3, 5, 7 ]
616 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Remove(l); l;</span>
617 7
618 [ 2, 3, 5 ]
619 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Remove(l,5); l;</span>
620 [ 2, 3, 5 ]
621 </pre></div>
622
623 <p><a id="X79D7E96F80A2D7C0" name="X79D7E96F80A2D7C0"></a></p>
624
625 <h5>21.4-4 CopyListEntries</h5>
626
627 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CopyListEntries</code>( <var class="Arg">fromlst</var>, <var class="Arg">fromind</var>, <var class="Arg">fromstep</var>, <var class="Arg">tolst</var>, <var class="Arg">toind</var>, <var class="Arg">tostep</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
628 <p>This function copies <var class="Arg">n</var> elements from <var class="Arg">fromlst</var>, starting at position <var class="Arg">fromind</var> and incrementing the position by <var class="Arg">fromstep</var> each time, into <var class="Arg">tolst</var> starting at position <var class="Arg">toind</var> and incrementing the position by <var class="Arg">tostep</var> each time. <var class="Arg">fromlst</var> and <var class="Arg">tolst</var> must be plain lists. <var class="Arg">fromstep</var> and/or <var class="Arg">tostep</var> can be negative. Unbound positions of <var class="Arg">fromlst</var> are simply copied to <var class="Arg">tolst</var>.</p>
629
630 <p><code class="func">CopyListEntries</code> is used in methods for the operations <code class="func">Add</code> (<a href="chap21.html#X795EC9D67E34DAB0"><span class="RefLink">21.4-2</span></a>) and <code class="func">Remove</code> (<a href="chap21.html#X7E98B11B79BA9167"><span class="RefLink">21.4-3</span></a>).</p>
631
632 <p><a id="X79E31DB27C82D6E1" name="X79E31DB27C82D6E1"></a></p>
633
634 <h5>21.4-5 Append</h5>
635
636 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Append</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
637 <p>adds the elements of the list <var class="Arg">list2</var> to the end of the mutable list <var class="Arg">list1</var>, see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>. <var class="Arg">list2</var> may contain holes, in which case the corresponding entries in <var class="Arg">list1</var> will be left unbound. <code class="func">Append</code> returns nothing, it is only called for its side effect.</p>
638
639 <p>Note that <code class="func">Append</code> changes its first argument, while <code class="func">Concatenation</code> (<a href="chap21.html#X840C55A77D1BB2E1"><span class="RefLink">21.20-1</span></a>) creates a new list and leaves its arguments unchanged.</p>
640
641
642 <div class="example"><pre>
643 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ 2, 3, 5 ];; Append( l, [ 7, 11, 13 ] ); l;</span>
644 [ 2, 3, 5, 7, 11, 13 ]
645 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Append( l, [ 17,, 23 ] ); l;</span>
646 [ 2, 3, 5, 7, 11, 13, 17,, 23 ]
647 </pre></div>
648
649 <p><a id="X7963C8E17EFF86DB" name="X7963C8E17EFF86DB"></a></p>
650
651 <h4>21.5 <span class="Heading">IsBound and Unbind for Lists</span></h4>
652
653 <p><a id="X79EC565A7DCEC938" name="X79EC565A7DCEC938"></a></p>
654
655 <h5>21.5-1 IsBound</h5>
656
657 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsBound</code>( <var class="Arg">list</var>[, <var class="Arg">n</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
658 <p><code class="func">IsBound</code> returns <code class="keyw">true</code> if the list <var class="Arg">list</var> has an element at index <var class="Arg">n</var>, and <code class="keyw">false</code> otherwise. <var class="Arg">list</var> must evaluate to a list, or to an object for which a suitable method for <code class="code">IsBound\[\]</code> has been installed, otherwise an error is signalled.</p>
659
660
661 <div class="example"><pre>
662 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ , 2, 3, , 5, , 7, , , , 11 ];;</span>
663 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsBound( l[7] );</span>
664 true
665 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsBound( l[4] );</span>
666 false
667 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsBound( l[101] );</span>
668 false
669 </pre></div>
670
671 <p><a id="X866F45D3797FDA00" name="X866F45D3797FDA00"></a></p>
672
673 <h5>21.5-2 GetWithDefault</h5>
674
675 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; GetWithDefault</code>( <var class="Arg">list</var>, <var class="Arg">n</var>, <var class="Arg">default</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
676 <p><code class="func">GetWithDefault</code> returns the <var class="Arg">n</var>th element of the list <var class="Arg">list</var>, if <var class="Arg">list</var> has a value at index <var class="Arg">n</var>, and <var class="Arg">default</var> otherwise.</p>
677
678 <p>While this method can be used on any list, it is particularly useful for Weak Pointer lists <a href="chap86.html#X86D963DC7968899B"><span class="RefLink">86.1</span></a> where the value of the list can change.</p>
679
680 <p>To distinguish between the <var class="Arg">n</var>th element being unbound, or <var class="Arg">default</var> being in <var class="Arg">list</var>, users can create a new mutable object, such as a string. <code class="func">IsIdenticalObj</code> (<a href="chap12.html#X7961183378DFB902"><span class="RefLink">12.5-1</span></a>) returns <code class="keyw">false</code> for different mutable strings, even if their contents are the same.</p>
681
682
683 <div class="example"><pre>
684 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [1,2,,"a"];</span>
685 [ 1, 2,, "a" ]
686 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">newobj := "a";</span>
687 "a"
688 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GetWithDefault(l, 2, newobj);</span>
689 2
690 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GetWithDefault(l, 3, newobj);</span>
691 "a"
692 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GetWithDefault(l, 4, newobj);</span>
693 "a"
694 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj(GetWithDefault(l, 3, newobj), newobj);</span>
695 true
696 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj(GetWithDefault(l, 4, newobj), newobj);</span>
697 false
698 </pre></div>
699
700 <p><a id="X78B72FDF7BD63C0B" name="X78B72FDF7BD63C0B"></a></p>
701
702 <h5>21.5-3 Unbind</h5>
703
704 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Unbind</code>( <var class="Arg">list</var>[, <var class="Arg">n</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
705 <p><code class="func">Unbind</code> deletes the element with index <var class="Arg">n</var> in the mutable list <var class="Arg">list</var>. That is, after execution of <code class="func">Unbind</code>, <var class="Arg">list</var> no longer has an assigned value with index <var class="Arg">n</var>. Thus <code class="func">Unbind</code> can be used to produce holes in a list. Note that it is not an error to unbind a nonexistent list element. <var class="Arg">list</var> must evaluate to a list, or to an object for which a suitable method for <code class="code">Unbind\[\]</code> has been installed, otherwise an error is signalled.</p>
706
707
708 <div class="example"><pre>
709 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [ , 2, 3, 5, , 7, , , , 11 ];;</span>
710 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Unbind( l[3] ); l;</span>
711 [ , 2,, 5,, 7,,,, 11 ]
712 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Unbind( l[4] ); l;</span>
713 [ , 2,,,, 7,,,, 11 ]
714 </pre></div>
715
716 <p>Note that <code class="func">IsBound</code> (<a href="chap21.html#X79EC565A7DCEC938"><span class="RefLink">21.5-1</span></a>) and <code class="func">Unbind</code> are special in that they do not evaluate their argument, otherwise <code class="func">IsBound</code> (<a href="chap21.html#X79EC565A7DCEC938"><span class="RefLink">21.5-1</span></a>) would always signal an error when it is supposed to return <code class="keyw">false</code> and there would be no way to tell <code class="func">Unbind</code> which component to remove.</p>
717
718 <p><a id="X7DD65BEA7EDB0CD7" name="X7DD65BEA7EDB0CD7"></a></p>
719
720 <h4>21.6 <span class="Heading">Identical Lists</span></h4>
721
722 <p>With the list assignment (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>) it is possible to change a mutable list. This section describes the semantic consequences of this fact. (See also <a href="chap12.html#X84545F3985C60F5B"><span class="RefLink">12.5</span></a>.)</p>
723
724 <p>First we define what it means when we say that <q>an object is changed</q>. You may think that in the following example the second assignment changes the integer.</p>
725
726
727 <div class="example"><pre>
728 i := 3;
729 i := i + 1;
730 </pre></div>
731
732 <p>But in this example it is not the <em>integer</em> <code class="code">3</code> which is changed, by adding one to it. Instead the <em>variable</em> <code class="code">i</code> is changed by assigning the value of <code class="code">i+1</code>, which happens to be <code class="code">4</code>, to <code class="code">i</code>. The same thing happens in the example below.</p>
733
734
735 <div class="example"><pre>
736 l := [ 1, 2 ];
737 l := [ 1, 2, 3 ];
738 </pre></div>
739
740 <p>The second assignment does not change the first list, instead it assigns a new list to the variable <code class="code">l</code>. On the other hand, in the following example the list <em>is</em> changed by the second assignment.</p>
741
742
743 <div class="example"><pre>
744 l := [ 1, 2 ];
745 l[3] := 3;
746 </pre></div>
747
748 <p>To understand the difference, think of a variable as a name for an object. The important point is that a list can have several names at the same time. An assignment <code class="code"><var class="Arg">var</var>:= <var class="Arg">list</var>;</code> means in this interpretation that <var class="Arg">var</var> is a name for the object <var class="Arg">list</var>. At the end of the following example <code class="code">l2</code> still has the value <code class="code">[ 1, 2 ]</code> as this list has not been changed and nothing else has been assigned to it.</p>
749
750
751 <div class="example"><pre>
752 l1 := [ 1, 2 ];
753 l2 := l1;
754 l1 := [ 1, 2, 3 ];
755 </pre></div>
756
757 <p>But after the following example the list for which <code class="code">l2</code> is a name has been changed and thus the value of <code class="code">l2</code> is now <code class="code">[ 1, 2, 3 ]</code>.</p>
758
759
760 <div class="example"><pre>
761 l1 := [ 1, 2 ];
762 l2 := l1;
763 l1[3] := 3;
764 </pre></div>
765
766 <p>We say that two lists are <em>identical</em> if changing one of them by a list assignment also changes the other one. This is slightly incorrect, because if <em>two</em> lists are identical, there are actually only two names for <em>one</em> list. However, the correct usage would be very awkward and would only add to the confusion. Note that two identical lists must be equal, because there is only one list with two different names. Thus identity is an equivalence relation that is a refinement of equality. Identity of objects can be detected using <code class="func">IsIdenticalObj</code> (<a href="chap12.html#X7961183378DFB902"><span class="RefLink">12.5-1</span></a>).</p>
767
768 <p>Let us now consider under which circumstances two lists are identical.</p>
769
770 <p>If you enter a list literal then the list denoted by this literal is a new list that is not identical to any other list. Thus in the following example <code class="code">l1</code> and <code class="code">l2</code> are not identical, though they are equal of course.</p>
771
772
773 <div class="example"><pre>
774 l1 := [ 1, 2 ];
775 l2 := [ 1, 2 ];
776 </pre></div>
777
778 <p>Also in the following example, no lists in the list <code class="code">l</code> are identical.</p>
779
780
781 <div class="example"><pre>
782 l := [];
783 for i in [1..10] do l[i] := [ 1, 2 ]; od;
784 </pre></div>
785
786 <p>If you assign a list to a variable no new list is created. Thus the list value of the variable on the left hand side and the list on the right hand side of the assignment are identical. So in the following example <code class="code">l1</code> and <code class="code">l2</code> are identical lists.</p>
787
788
789 <div class="example"><pre>
790 l1 := [ 1, 2 ];
791 l2 := l1;
792 </pre></div>
793
794 <p>If you pass a list as an argument, the old list and the argument of the function are identical. Also if you return a list from a function, the old list and the value of the function call are identical. So in the following example <code class="code">l1</code> and <code class="code">l2</code> are identical lists:</p>
795
796
797 <div class="example"><pre>
798 l1 := [ 1, 2 ];
799 f := function ( l ) return l; end;
800 l2 := f( l1 );
801 </pre></div>
802
803 <p>If you change a list it keeps its identity. Thus if two lists are identical and you change one of them, you also change the other, and they are still identical afterwards. On the other hand, two lists that are not identical will never become identical if you change one of them. So in the following example both <code class="code">l1</code> and <code class="code">l2</code> are changed, and are still identical.</p>
804
805
806 <div class="example"><pre>
807 l1 := [ 1, 2 ];
808 l2 := l1;
809 l1[1] := 2;
810 </pre></div>
811
812 <p><a id="X7ED7C0738495556F" name="X7ED7C0738495556F"></a></p>
813
814 <h4>21.7 <span class="Heading">Duplication of Lists</span></h4>
815
816 <p>Here we describe the meaning of <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) and <code class="func">StructuralCopy</code> (<a href="chap12.html#X7C1E70587EBDD2CB"><span class="RefLink">12.7-2</span></a>) for lists. For the general definition of these functions, see <a href="chap12.html#X786B942B82D684BD"><span class="RefLink">12.7</span></a>.</p>
817
818 <p>The subobjects (see <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>)) of a list are exactly its elements.</p>
819
820 <p>This means that for any list <var class="Arg">list</var>, <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) returns a mutable <em>new</em> list <var class="Arg">new</var> that is <em>not identical</em> to any other list (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>), and whose elements are identical to the elements of <var class="Arg">list</var>.</p>
821
822 <p>Analogously, for a <em>mutable</em> list <var class="Arg">list</var>, <code class="func">StructuralCopy</code> (<a href="chap12.html#X7C1E70587EBDD2CB"><span class="RefLink">12.7-2</span></a>) returns a mutable <em>new</em> list <var class="Arg">scp</var> that is <em>not identical</em> to any other list, and whose elements are structural copies (defined recursively) of the elements of <var class="Arg">list</var>; an element of <var class="Arg">scp</var> is mutable (and then a <em>new</em> list) if and only if the corresponding element of <var class="Arg">list</var> is mutable.</p>
823
824 <p>In both cases, modifying the copy <var class="Arg">new</var> resp. <var class="Arg">scp</var> by assignments (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>) does not modify the original object <var class="Arg">list</var>.</p>
825
826 <p><code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) basically executes the following code for lists.</p>
827
828
829 <div class="example"><pre>
830 new := [];
831 for i in [ 1 .. Length( list ) ] do
832 if IsBound( list[i] ) then
833 new[i] := list[i];
834 fi;
835 od;
836 </pre></div>
837
838
839 <div class="example"><pre>
840 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ [ 1, 2 ], [ 3, 4 ] ];; list2 := ShallowCopy( list1 );;</span>
841 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj( list1, list2 );</span>
842 false
843 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj( list1[1], list2[1] );</span>
844 true
845 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2[1] := 0;; list1; list2;</span>
846 [ [ 1, 2 ], [ 3, 4 ] ]
847 [ 0, [ 3, 4 ] ]
848 </pre></div>
849
850 <p><code class="func">StructuralCopy</code> (<a href="chap12.html#X7C1E70587EBDD2CB"><span class="RefLink">12.7-2</span></a>) basically executes the following code for lists.</p>
851
852
853 <div class="example"><pre>
854 new := [];
855 for i in [ 1 .. Length( list ) ] do
856 if IsBound( list[i] ) then
857 new[i] := StructuralCopy( list[i] );
858 fi;
859 od;
860 </pre></div>
861
862
863 <div class="example"><pre>
864 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ [ 1, 2 ], [ 3, 4 ] ];; list2 := StructuralCopy( list1 );;</span>
865 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj( list1, list2 );</span>
866 false
867 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIdenticalObj( list1[1], list2[1] );</span>
868 false
869 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2[1][1] := 0;; list1; list2;</span>
870 [ [ 1, 2 ], [ 3, 4 ] ]
871 [ [ 0, 2 ], [ 3, 4 ] ]
872 </pre></div>
873
874 <p>The above code is not entirely correct. If the object <var class="Arg">list</var> contains a mutable object twice this object is not copied twice, as would happen with the above definition, but only once. This means that the copy <var class="Arg">new</var> and the object <var class="Arg">list</var> have exactly the same structure when viewed as a general graph.</p>
875
876
877 <div class="example"><pre>
878 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sub := [ 1, 2 ];; list1 := [ sub, sub ];;</span>
879 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2 := StructuralCopy( list1 );</span>
880 [ [ 1, 2 ], [ 1, 2 ] ]
881 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2[1][1] := 0;; list2;</span>
882 [ [ 0, 2 ], [ 0, 2 ] ]
883 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1;</span>
884 [ [ 1, 2 ], [ 1, 2 ] ]
885 </pre></div>
886
887 <p><a id="X808A207182B2F84F" name="X808A207182B2F84F"></a></p>
888
889 <h4>21.8 <span class="Heading">Membership Test for Lists</span></h4>
890
891 <p><a id="X7B914A287F88ED0A" name="X7B914A287F88ED0A"></a></p>
892
893 <h5><code>21.8-1 \in</code></h5>
894
895 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \in</code>( <var class="Arg">obj</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
896 <p>This function call or the infix variant <var class="Arg">obj</var> <code class="keyw">in</code> <var class="Arg">list</var> tests whether there is a positive integer <span class="SimpleMath">i</span> such that <var class="Arg">list</var><span class="SimpleMath">[i] =</span> <var class="Arg">obj</var> holds.</p>
897
898 <p>If the list <var class="Arg">list</var> knows that it is strictly sorted (see <code class="func">IsSSortedList</code> (<a href="chap21.html#X80CDAF45782E8DCB"><span class="RefLink">21.17-4</span></a>)), the membership test is much quicker, because a binary search can be used instead of the linear search used for arbitrary lists, see <code class="func">\in</code> (<a href="chap21.html#X7B16AD597CB12305"><span class="RefLink">21.19-1</span></a>).</p>
899
900
901 <div class="example"><pre>
902 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">1 in [ 2, 2, 1, 3 ]; 1 in [ 4, -1, 0, 3 ];</span>
903 true
904 false
905 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := SSortedList( [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32] );;</span>
906 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">17 in s; # uses binary search and only 4 comparisons</span>
907 false
908 </pre></div>
909
910 <p>For finding the position of an element in a list, see <a href="chap21.html#X8196FD4779BCCA0C"><span class="RefLink">21.16</span></a>.</p>
911
912 <p><a id="X84D6FC7E7E39ED33" name="X84D6FC7E7E39ED33"></a></p>
913
914 <h4>21.9 <span class="Heading">Enlarging Internally Represented Lists</span></h4>
915
916 <p>Section <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a> told you (among other things) that it is possible to assign beyond the logical end of a mutable list, automatically enlarging the list. This section tells you how this is done for internally represented lists.</p>
917
918 <p>It would be extremely wasteful to make all lists large enough so that there is room for all assignments, because some lists may have more than 100000 elements, while most lists have less than 10 elements.</p>
919
920 <p>On the other hand suppose every assignment beyond the end of a list would be done by allocating new space for the list and copying all entries to the new space. Then creating a list of 1000 elements by assigning them in order, would take half a million copy operations and also create a lot of garbage that the garbage collector would have to reclaim.</p>
921
922 <p>So the following strategy is used. If a list is created it is created with exactly the correct size. If a list is enlarged, because of an assignment beyond the end of the list, it is enlarged by at least <code class="code"><var class="Arg">length</var>/8 + 4</code> entries. Therefore the next assignments beyond the end of the list do not need to enlarge the list. For example creating a list of 1000 elements by assigning them in order, would now take only 32 enlargements.</p>
923
924 <p>The result of this is of course that the <em>physical length</em> of a list may be larger than the <em>logical length</em>, which is usually called simply the length of the list. Aside from the implications for the performance you need not be aware of the physical length. In fact all you can ever observe, for example by calling <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), is the logical length.</p>
925
926 <p>Suppose that <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>) would have to take the physical length and then test how many entries at the end of a list are unassigned, to compute the logical length of the list. That would take too much time. In order to make <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), and other functions that need to know the logical length, more efficient, the length of a list is stored along with the list.</p>
927
928 <p>For fine tuning code dealing with plain lists we provide the following two functions.</p>
929
930 <p><a id="X78BF67A5802E93AD" name="X78BF67A5802E93AD"></a></p>
931
932 <h5>21.9-1 EmptyPlist</h5>
933
934 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; EmptyPlist</code>( <var class="Arg">len</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
935 <p>Returns: a plain list</p>
936
937 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ShrinkAllocationPlist</code>( <var class="Arg">l</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
938 <p>Returns: nothing</p>
939
940 <p>The function <code class="func">EmptyPlist</code> returns an empty plain list which has enough memory allocated for <var class="Arg">len</var> entries. This can be useful for creating and filling a plain list with a known number of entries.</p>
941
942 <p>The function <code class="func">ShrinkAllocationPlist</code> gives back to <strong class="pkg">GAP</strong>'s memory manager the physical memory which is allocated for the plain list <var class="Arg">l</var> but not needed by the current number of entries.</p>
943
944 <p>Note that there are similar functions <code class="func">EmptyString</code> (<a href="chap27.html#X836078DC829A8221"><span class="RefLink">27.4-5</span></a>) and <code class="func">ShrinkAllocationString</code> (<a href="chap27.html#X836078DC829A8221"><span class="RefLink">27.4-5</span></a>) for strings instead of plain lists.</p>
945
946
947 <div class="example"><pre>
948 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=[]; for i in [1..160] do Add(l, i^2); od;</span>
949 [ ]
950 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:=EmptyPlist(160); for i in [1..160] do Add(m, i^2); od;</span>
951 [ ]
952 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># now l uses about 25% more memory than the equal list m</span>
953 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ShrinkAllocationPlist(l);</span>
954 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># now l and m use the same amount of memory</span>
955 </pre></div>
956
957 <p><a id="X8016D50F85147A77" name="X8016D50F85147A77"></a></p>
958
959 <h4>21.10 <span class="Heading">Comparisons of Lists</span></h4>
960
961 <p><code class="code"><var class="Arg">list1</var> = <var class="Arg">list2</var></code></p>
962
963 <p><code class="code"><var class="Arg">list1</var> &lt;&gt; <var class="Arg">list2</var></code></p>
964
965 <p>Two lists <var class="Arg">list1</var> and <var class="Arg">list2</var> are equal if and only if for every index <span class="SimpleMath">i</span>, either both entries <var class="Arg">list1</var><span class="SimpleMath">[i]</span> and <var class="Arg">list2</var><span class="SimpleMath">[i]</span> are unbound, or both are bound and are equal, i.e., <var class="Arg">list1</var><span class="SimpleMath">[i] =</span> <var class="Arg">list2</var><span class="SimpleMath">[i]</span> is <code class="keyw">true</code>.</p>
966
967
968 <div class="example"><pre>
969 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ] = [ 1, 2, 3 ];</span>
970 true
971 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ , 2, 3 ] = [ 1, 2, ];</span>
972 false
973 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ] = [ 3, 2, 1 ];</span>
974 false
975 </pre></div>
976
977 <p>This definition will cause problems with lists which are their own entries. Comparing two such lists for equality may lead to an infinite recursion in the kernel if the list comparison has to compare the list entries which are in fact the lists themselves, and then <strong class="pkg">GAP</strong> crashes.</p>
978
979 <p><code class="code"><var class="Arg">list1</var> &lt; <var class="Arg">list2</var></code></p>
980
981 <p><code class="code"><var class="Arg">list1</var> &lt;= <var class="Arg">list2</var></code></p>
982
983 <p>Lists are ordered <em>lexicographically</em>. Unbound entries are smaller than any bound entry. That implies the following behaviour. Let <span class="SimpleMath">i</span> be the smallest positive integer <span class="SimpleMath">i</span> such that <var class="Arg">list1</var> and <var class="Arg">list2</var> at position <span class="SimpleMath">i</span> differ, i.e., either exactly one of <var class="Arg">list1</var><span class="SimpleMath">[i]</span>, <var class="Arg">list2</var><span class="SimpleMath">[i]</span> is bound or both entries are bound and differ. Then <var class="Arg">list1</var> is less than <var class="Arg">list2</var> if either <var class="Arg">list1</var><span class="SimpleMath">[i]</span> is unbound (and <var class="Arg">list2</var><span class="SimpleMath">[i]</span> is not) or both are bound and <var class="Arg">list1</var><span class="SimpleMath">[i]</span> &lt; <var class="Arg">list2</var><span class="SimpleMath">[i]</span> is <code class="keyw">true</code>.</p>
984
985
986 <div class="example"><pre>
987 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3, 4 ] &lt; [ 1, 2, 4, 8 ]; # &lt;list1&gt;[3] &lt; &lt;list2&gt;[3]</span>
988 true
989 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ] &lt; [ 1, 2, 3, 5 ]; # &lt;list1&gt;[4] is unbound and thus &lt; 5</span>
990 true
991 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, , 3, 4 ] &lt; [ 1, -1, 3 ]; # &lt;list1&gt;[2] is unbound and thus &lt; -1</span>
992 true
993 </pre></div>
994
995 <p>Note that for comparing two lists with <code class="code">&lt;</code> or <code class="code">&lt;=</code>, the (relevant) list elements must be comparable with <code class="code">&lt;</code>, which is usually <em>not</em> the case for objects in different families, see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>. Also for the possibility to compare lists with other objects, see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>.</p>
996
997 <p><a id="X845EEAF083D43CCE" name="X845EEAF083D43CCE"></a></p>
998
999 <h4>21.11 <span class="Heading">Arithmetic for Lists</span></h4>
1000
1001 <p>It is convenient to have arithmetic operations for lists, in particular because in <strong class="pkg">GAP</strong> row vectors and matrices are special kinds of lists. However, it is the wide variety of list objects because of which we prescribe arithmetic operations <em>not for all</em> of them. (Keep in mind that <q>list</q> means just an object in the category <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>).)</p>
1002
1003 <p>(Due to the intended generality and flexibility, the definitions given in the following sections are quite technical. But for not too complicated cases such as matrices (see <a href="chap24.html#X7899335779A39A95"><span class="RefLink">24.3</span></a>) and row vectors (see <a href="chap23.html#X85516C3179C229DB"><span class="RefLink">23.2</span></a>) whose entries aren't lists, the resulting behaviour should be intuitive.)</p>
1004
1005 <p>For example, we want to deal with matrices which can be added and multiplied in the usual way, via the infix operators <code class="code">+</code> and <code class="code">*</code>; and we want also Lie matrices, with the same additive behaviour but with the multiplication defined by the Lie bracket. Both kinds of matrices shall be lists, with the usual access to their rows, with <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>) returning the number of rows etc.</p>
1006
1007 <p>For the categories and attributes that control the arithmetic behaviour of lists, see <a href="chap21.html#X84D642967B8546B7"><span class="RefLink">21.12</span></a>.</p>
1008
1009 <p>For the definition of return values of additive and multiplicative operations whose arguments are lists in these filters, see <a href="chap21.html#X7E6A1F66781BE923"><span class="RefLink">21.13</span></a> and <a href="chap21.html#X782ED7F27D8C7FC1"><span class="RefLink">21.14</span></a>, respectively. It should be emphasized that these sections describe only what the return values are, and not how they are computed.</p>
1010
1011 <p>For the mutability status of the return values, see <a href="chap21.html#X8676EFE67972FD06"><span class="RefLink">21.15</span></a>. (Note that this is not dealt with in the sections about the result values.)</p>
1012
1013 <p>Further details about the special cases of row vectors and matrices can be found in <a href="chap23.html#X85516C3179C229DB"><span class="RefLink">23.2</span></a> and in <a href="chap24.html#X7899335779A39A95"><span class="RefLink">24.3</span></a>, the compression status is dealt with in <a href="chap23.html#X8679F7DD7DFCBD9C"><span class="RefLink">23.3</span></a> and <a href="chap24.html#X873822B6830CE367"><span class="RefLink">24.14</span></a>.</p>
1014
1015 <p><a id="X84D642967B8546B7" name="X84D642967B8546B7"></a></p>
1016
1017 <h4>21.12 <span class="Heading">Filters Controlling the Arithmetic Behaviour of Lists</span></h4>
1018
1019 <p>The arithmetic behaviour of lists is controlled by their types. The following categories and attributes are used for that.</p>
1020
1021 <p>Note that we distinguish additive and multiplicative behaviour. For example, Lie matrices have the usual additive behaviour but not the usual multiplicative behaviour.</p>
1022
1023 <p><a id="X87ABCEE9809585A0" name="X87ABCEE9809585A0"></a></p>
1024
1025 <h5>21.12-1 IsGeneralizedRowVector</h5>
1026
1027 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsGeneralizedRowVector</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1028 <p>For a list <var class="Arg">list</var>, the value <code class="keyw">true</code> for <code class="func">IsGeneralizedRowVector</code> indicates that the additive arithmetic behaviour of <var class="Arg">list</var> is as defined in <a href="chap21.html#X7E6A1F66781BE923"><span class="RefLink">21.13</span></a>, and that the attribute <code class="func">NestingDepthA</code> (<a href="chap21.html#X8428E77B86722D52"><span class="RefLink">21.12-4</span></a>) will return a nonzero value when called with <var class="Arg">list</var>.</p>
1029
1030
1031 <div class="example"><pre>
1032 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsList( "abc" ); IsGeneralizedRowVector( "abc" );</span>
1033 true
1034 false
1035 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">liemat:= LieObject( [ [ 1, 2 ], [ 3, 4 ] ] );</span>
1036 LieObject( [ [ 1, 2 ], [ 3, 4 ] ] )
1037 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsGeneralizedRowVector( liemat );</span>
1038 true
1039 </pre></div>
1040
1041 <p><a id="X7FBCA5B58308C158" name="X7FBCA5B58308C158"></a></p>
1042
1043 <h5>21.12-2 IsMultiplicativeGeneralizedRowVector</h5>
1044
1045 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsMultiplicativeGeneralizedRowVector</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1046 <p>For a list <var class="Arg">list</var>, the value <code class="keyw">true</code> for <code class="func">IsMultiplicativeGeneralizedRowVector</code> indicates that the multiplicative arithmetic behaviour of <var class="Arg">list</var> is as defined in <a href="chap21.html#X782ED7F27D8C7FC1"><span class="RefLink">21.14</span></a>, and that the attribute <code class="func">NestingDepthM</code> (<a href="chap21.html#X84B383B97FD986CD"><span class="RefLink">21.12-5</span></a>) will return a nonzero value when called with <var class="Arg">list</var>.</p>
1047
1048
1049 <div class="example"><pre>
1050 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMultiplicativeGeneralizedRowVector( liemat );</span>
1051 false
1052 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">bas:= CanonicalBasis( FullRowSpace( Rationals, 3 ) );</span>
1053 CanonicalBasis( ( Rationals^3 ) )
1054 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMultiplicativeGeneralizedRowVector( bas );</span>
1055 true
1056 </pre></div>
1057
1058 <p>Note that the filters <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), <code class="func">IsMultiplicativeGeneralizedRowVector</code> do <em>not</em> enable default methods for addition or multiplication (cf. <code class="func">IsListDefault</code> (<a href="chap21.html#X7BAD12E67BFC90DE"><span class="RefLink">21.12-3</span></a>)).</p>
1059
1060 <p><a id="X7BAD12E67BFC90DE" name="X7BAD12E67BFC90DE"></a></p>
1061
1062 <h5>21.12-3 IsListDefault</h5>
1063
1064 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsListDefault</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1065 <p>For a list <var class="Arg">list</var>, <code class="func">IsListDefault</code> indicates that the default methods for arithmetic operations of lists, such as pointwise addition and multiplication as inner product or matrix product, shall be applicable to <var class="Arg">list</var>.</p>
1066
1067 <p><code class="func">IsListDefault</code> implies <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) and <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>).</p>
1068
1069 <p>All internally represented lists are in this category, and also all lists in the representations <code class="code">IsGF2VectorRep</code>, <code class="code">Is8BitVectorRep</code>, <code class="code">IsGF2MatrixRep</code>, and <code class="code">Is8BitMatrixRep</code> (see <a href="chap23.html#X8679F7DD7DFCBD9C"><span class="RefLink">23.3</span></a> and <a href="chap24.html#X873822B6830CE367"><span class="RefLink">24.14</span></a>). Note that the result of an arithmetic operation with lists in <code class="func">IsListDefault</code> will in general be an internally represented list, so most <q>wrapped list objects</q> will not lie in <code class="func">IsListDefault</code>.</p>
1070
1071
1072 <div class="example"><pre>
1073 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">v:= [ 1, 2 ];; m:= [ v, 2*v ];;</span>
1074 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsListDefault( v ); IsListDefault( m );</span>
1075 true
1076 true
1077 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsListDefault( bas ); IsListDefault( liemat );</span>
1078 true
1079 false
1080 </pre></div>
1081
1082 <p><a id="X8428E77B86722D52" name="X8428E77B86722D52"></a></p>
1083
1084 <h5>21.12-4 NestingDepthA</h5>
1085
1086 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NestingDepthA</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
1087 <p>For a <strong class="pkg">GAP</strong> object <var class="Arg">obj</var>, <code class="func">NestingDepthA</code> returns the <em>additive nesting depth</em> of <var class="Arg">obj</var>. This is defined recursively as the integer <span class="SimpleMath">0</span> if <var class="Arg">obj</var> is not in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), as the integer <span class="SimpleMath">1</span> if <var class="Arg">obj</var> is an empty list in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), and as <span class="SimpleMath">1</span> plus the additive nesting depth of the first bound entry in <var class="Arg">obj</var> otherwise.</p>
1088
1089 <p><a id="X84B383B97FD986CD" name="X84B383B97FD986CD"></a></p>
1090
1091 <h5>21.12-5 NestingDepthM</h5>
1092
1093 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NestingDepthM</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
1094 <p>For a <strong class="pkg">GAP</strong> object <var class="Arg">obj</var>, <code class="func">NestingDepthM</code> returns the <em>multiplicative nesting depth</em> of <var class="Arg">obj</var>. This is defined recursively as the integer <span class="SimpleMath">0</span> if <var class="Arg">obj</var> is not in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>), as the integer <span class="SimpleMath">1</span> if <var class="Arg">obj</var> is an empty list in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>), and as <span class="SimpleMath">1</span> plus the multiplicative nesting depth of the first bound entry in <var class="Arg">obj</var> otherwise.</p>
1095
1096
1097 <div class="example"><pre>
1098 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( v ); NestingDepthM( v );</span>
1099 1
1100 1
1101 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( m ); NestingDepthM( m );</span>
1102 2
1103 2
1104 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( liemat ); NestingDepthM( liemat );</span>
1105 2
1106 0
1107 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1:= [ [ 1, 2 ], 3 ];; l2:= [ 1, [ 2, 3 ] ];;</span>
1108 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( l1 ); NestingDepthM( l1 );</span>
1109 2
1110 2
1111 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( l2 ); NestingDepthM( l2 );</span>
1112 1
1113 1
1114 </pre></div>
1115
1116 <p><a id="X7E6A1F66781BE923" name="X7E6A1F66781BE923"></a></p>
1117
1118 <h4>21.13 <span class="Heading">Additive Arithmetic for Lists</span></h4>
1119
1120 <p>In this general context, we define the results of additive operations only in the following situations. For unary operations (zero and additive inverse), the unique argument must be in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>); for binary operations (addition and subtraction), at least one argument must be in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), and the other either is not a list or also in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>).</p>
1121
1122 <p>(For non-list <strong class="pkg">GAP</strong> objects, defining the results of unary operations is not an issue here, and if at least one argument is a list not in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), it shall be left to this argument whether the result in question is defined and what it is.)</p>
1123
1124 <p><a id="X86A85ADC85C451DC" name="X86A85ADC85C451DC"></a></p>
1125
1126 <h5>21.13-1 <span class="Heading">Zero for lists</span></h5>
1127
1128 <p>The zero (see <code class="func">Zero</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>)) of a list <span class="SimpleMath">x</span> in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) is defined as the list whose entry at position <span class="SimpleMath">i</span> is the zero of <span class="SimpleMath">x[i]</span> if this entry is bound, and is unbound otherwise.</p>
1129
1130
1131 <div class="example"><pre>
1132 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Zero( [ 1, 2, 3 ] ); Zero( [ [ 1, 2 ], 3 ] ); Zero( liemat );</span>
1133 [ 0, 0, 0 ]
1134 [ [ 0, 0 ], 0 ]
1135 LieObject( [ [ 0, 0 ], [ 0, 0 ] ] )
1136 </pre></div>
1137
1138 <p><a id="X7B91CE4D814C2D08" name="X7B91CE4D814C2D08"></a></p>
1139
1140 <h5>21.13-2 <span class="Heading">AdditiveInverse for lists</span></h5>
1141
1142 <p>The additive inverse (see <code class="func">AdditiveInverse</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>)) of a list <span class="SimpleMath">x</span> in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) is defined as the list whose entry at position <span class="SimpleMath">i</span> is the additive inverse of <span class="SimpleMath">x[i]</span> if this entry is bound, and is unbound otherwise.</p>
1143
1144
1145 <div class="example"><pre>
1146 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AdditiveInverse( [ 1, 2, 3 ] ); AdditiveInverse( [ [ 1, 2 ], 3 ] );</span>
1147 [ -1, -2, -3 ]
1148 [ [ -1, -2 ], -3 ]
1149 </pre></div>
1150
1151 <p><a id="X842D123E7EE5E3DB" name="X842D123E7EE5E3DB"></a></p>
1152
1153 <h5>21.13-3 <span class="Heading">Addition of lists</span></h5>
1154
1155 <p>If <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> are in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) and have the same additive nesting depth (see <code class="func">NestingDepthA</code> (<a href="chap21.html#X8428E77B86722D52"><span class="RefLink">21.12-4</span></a>)), the sum <span class="SimpleMath">x + y</span> is defined <em>pointwise</em>, in the sense that the result is a list whose entry at position <span class="SimpleMath">i</span> is <span class="SimpleMath">x[i] + y[i]</span> if these entries are bound, is a shallow copy (see <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>)) of <span class="SimpleMath">x[i]</span> or <span class="SimpleMath">y[i]</span> if the other argument is not bound at position <span class="SimpleMath">i</span>, and is unbound if both <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> are unbound at position <span class="SimpleMath">i</span>.</p>
1156
1157 <p>If <span class="SimpleMath">x</span> is in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) and <span class="SimpleMath">y</span> is in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) and has lower additive nesting depth, or is neither a list nor a domain, the sum <span class="SimpleMath">x + y</span> is defined as a list whose entry at position <span class="SimpleMath">i</span> is <span class="SimpleMath">x[i] + y</span> if <span class="SimpleMath">x</span> is bound at position <span class="SimpleMath">i</span>, and is unbound if not. The equivalent holds in the reversed case, where the order of the summands is kept, as addition is not always commutative.</p>
1158
1159
1160 <div class="example"><pre>
1161 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">1 + [ 1, 2, 3 ]; [ 1, 2, 3 ] + [ 0, 2, 4 ]; [ 1, 2 ] + [ Z(2) ];</span>
1162 [ 2, 3, 4 ]
1163 [ 1, 4, 7 ]
1164 [ 0*Z(2), 2 ]
1165 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1:= [ 1, , 3, 4 ];; l2:= [ , 2, 3, 4, 5 ];;</span>
1166 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l3:= [ [ 1, 2 ], , [ 5, 6 ] ];; l4:= [ , [ 3, 4 ], [ 5, 6 ] ];;</span>
1167 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( l1 ); NestingDepthA( l2 );</span>
1168 1
1169 1
1170 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NestingDepthA( l3 ); NestingDepthA( l4 );</span>
1171 2
1172 2
1173 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 + l2;</span>
1174 [ 1, 2, 6, 8, 5 ]
1175 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 + l3;</span>
1176 [ [ 2, 2, 3, 4 ],, [ 6, 6, 3, 4 ] ]
1177 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l2 + l4;</span>
1178 [ , [ 3, 6, 3, 4, 5 ], [ 5, 8, 3, 4, 5 ] ]
1179 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l3 + l4;</span>
1180 [ [ 1, 2 ], [ 3, 4 ], [ 10, 12 ] ]
1181 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 + [];</span>
1182 [ 1,, 3, 4 ]
1183 </pre></div>
1184
1185 <p><a id="X7C3DC8BE78DEECDE" name="X7C3DC8BE78DEECDE"></a></p>
1186
1187 <h5>21.13-4 <span class="Heading">Subtraction of lists</span></h5>
1188
1189 <p>For two <strong class="pkg">GAP</strong> objects <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> of which one is in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) and the other is also in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>) or is neither a list nor a domain, <span class="SimpleMath">x - y</span> is defined as <span class="SimpleMath">x + (-y)</span>.</p>
1190
1191
1192 <div class="example"><pre>
1193 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 - l2;</span>
1194 [ 1, -2, 0, 0, -5 ]
1195 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 - l3;</span>
1196 [ [ 0, -2, 3, 4 ],, [ -4, -6, 3, 4 ] ]
1197 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l2 - l4;</span>
1198 [ , [ -3, -2, 3, 4, 5 ], [ -5, -4, 3, 4, 5 ] ]
1199 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l3 - l4;</span>
1200 [ [ 1, 2 ], [ -3, -4 ], [ 0, 0 ] ]
1201 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 - [];</span>
1202 [ 1,, 3, 4 ]
1203 </pre></div>
1204
1205 <p><a id="X782ED7F27D8C7FC1" name="X782ED7F27D8C7FC1"></a></p>
1206
1207 <h4>21.14 <span class="Heading">Multiplicative Arithmetic for Lists</span></h4>
1208
1209 <p>In this general context, we define the results of multiplicative operations only in the following situations. For unary operations (one and inverse), the unique argument must be in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>); for binary operations (multiplication and division), at least one argument must be in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>), and the other either not a list or also in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>).</p>
1210
1211 <p>(For non-list <strong class="pkg">GAP</strong> objects, defining the results of unary operations is not an issue here, and if at least one argument is a list not in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>), it shall be left to this argument whether the result in question is defined and what it is.)</p>
1212
1213 <p><a id="X79A8A5627FD42FA5" name="X79A8A5627FD42FA5"></a></p>
1214
1215 <h5>21.14-1 <span class="Heading">One for lists</span></h5>
1216
1217 <p>The one (see <code class="func">One</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>)) of a dense list <var class="Arg">x</var> in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) such that <var class="Arg">x</var> has even multiplicative nesting depth and has the same length as each of its rows is defined as the usual identity matrix on the outer two levels, that is, an identity matrix of the same dimensions, with diagonal entries <code class="code">One( <var class="Arg">x</var>[1][1] )</code> and off-diagonal entries <code class="code">Zero( <var class="Arg">x</var>[1][1] )</code>.</p>
1218
1219
1220 <div class="example"><pre>
1221 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">One( [ [ 1, 2 ], [ 3, 4 ] ] );</span>
1222 [ [ 1, 0 ], [ 0, 1 ] ]
1223 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">One( [ [ [ [ 1 ] ], [ [ 2 ] ] ], [ [ [ 3 ] ], [ [ 4 ] ] ] ] );</span>
1224 [ [ [ [ 1 ] ], [ [ 0 ] ] ], [ [ [ 0 ] ], [ [ 1 ] ] ] ]
1225 </pre></div>
1226
1227 <p><a id="X78C6C1E2849D303A" name="X78C6C1E2849D303A"></a></p>
1228
1229 <h5>21.14-2 <span class="Heading">Inverse for lists</span></h5>
1230
1231 <p>The inverse (see <code class="func">Inverse</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>)) of an invertible square table <var class="Arg">x</var> in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) whose entries lie in a common field is defined as the usual inverse <span class="SimpleMath">y</span>, i.e., a square matrix over the same field such that <span class="SimpleMath"><var class="Arg">x</var> y</span> and <span class="SimpleMath">y <var class="Arg">x</var></span> is equal to <code class="code">One( <var class="Arg">x</var> )</code>.</p>
1232
1233
1234 <div class="example"><pre>
1235 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Inverse( [ [ 1, 2 ], [ 3, 4 ] ] );</span>
1236 [ [ -2, 1 ], [ 3/2, -1/2 ] ]
1237 </pre></div>
1238
1239 <p><a id="X84FDB95179BFE4CD" name="X84FDB95179BFE4CD"></a></p>
1240
1241 <h5>21.14-3 <span class="Heading">Multiplication of lists</span></h5>
1242
1243 <p>There are three possible computations that might be triggered by a multiplication involving a list in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>). Namely, <span class="SimpleMath">x * y</span> might be</p>
1244
1245
1246 <dl>
1247 <dt><strong class="Mark">(I)</strong></dt>
1248 <dd><p>the inner product <span class="SimpleMath">x[1] * y[1] + x[2] * y[2] + ⋯ + x[n] * y[n]</span>, where summands are omitted for which the entry in <span class="SimpleMath">x</span> or <span class="SimpleMath">y</span> is unbound (if this leaves no summand then the multiplication is an error), or</p>
1249
1250 </dd>
1251 <dt><strong class="Mark">(L)</strong></dt>
1252 <dd><p>the left scalar multiple, i.e., a list whose entry at position <span class="SimpleMath">i</span> is <span class="SimpleMath">x * y[i]</span> if <span class="SimpleMath">y</span> is bound at position <span class="SimpleMath">i</span>, and is unbound if not, or</p>
1253
1254 </dd>
1255 <dt><strong class="Mark">(R)</strong></dt>
1256 <dd><p>the right scalar multiple, i.e., a list whose entry at position <span class="SimpleMath">i</span> is <span class="SimpleMath">x[i] * y</span> if <span class="SimpleMath">x</span> is bound at position <span class="SimpleMath">i</span>, and is unbound if not.</p>
1257
1258 </dd>
1259 </dl>
1260 <p>Our aim is to generalize the basic arithmetic of simple row vectors and matrices, so we first summarize the situations that shall be covered.</p>
1261
1262 <div class="pcenter"><table class="GAPDocTable">
1263 <tr>
1264 <td class="tdcenter"></td>
1265 <td class="tdcenter">scl</td>
1266 <td class="tdcenter">vec</td>
1267 <td class="tdcenter">mat</td>
1268 </tr>
1269 <tr>
1270 <td class="tdcenter">scl</td>
1271 <td class="tdcenter"></td>
1272 <td class="tdcenter">(L)</td>
1273 <td class="tdcenter">(L)</td>
1274 </tr>
1275 <tr>
1276 <td class="tdcenter">vec</td>
1277 <td class="tdcenter">(R)</td>
1278 <td class="tdcenter">(I)</td>
1279 <td class="tdcenter">(I)</td>
1280 </tr>
1281 <tr>
1282 <td class="tdcenter">mat</td>
1283 <td class="tdcenter">(R)</td>
1284 <td class="tdcenter">(R)</td>
1285 <td class="tdcenter">(R)</td>
1286 </tr>
1287 </table><br />
1288 </div>
1289
1290 <p>This means for example that the product of a scalar (scl) with a vector (vec) or a matrix (mat) is computed according to (L). Note that this is asymmetric.</p>
1291
1292 <p>Now we can state the general multiplication rules.</p>
1293
1294 <p>If exactly one argument is in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) then we regard the other argument (which is then neither a list nor a domain) as a scalar, and specify result (L) or (R), depending on ordering.</p>
1295
1296 <p>In the remaining cases, both <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> are in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>), and we distinguish the possibilities by their multiplicative nesting depths. An argument with <em>odd</em> multiplicative nesting depth is regarded as a vector, and an argument with <em>even</em> multiplicative nesting depth is regarded as a scalar or a matrix.</p>
1297
1298 <p>So if both arguments have odd multiplicative nesting depth, we specify result (I).</p>
1299
1300 <p>If exactly one argument has odd nesting depth, the other is treated as a scalar if it has lower multiplicative nesting depth, and as a matrix otherwise. In the former case, we specify result (L) or (R), depending on ordering; in the latter case, we specify result (L) or (I), depending on ordering.</p>
1301
1302 <p>We are left with the case that each argument has even multiplicative nesting depth. If the two depths are equal, we treat the computation as a matrix product, and specify result (R). Otherwise, we treat the less deeply nested argument as a scalar and the other as a matrix, and specify result (L) or (R), depending on ordering.</p>
1303
1304
1305 <div class="example"><pre>
1306 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ (), (2,3), (1,2), (1,2,3), (1,3,2), (1,3) ] * (1,4);</span>
1307 [ (1,4), (1,4)(2,3), (1,2,4), (1,2,3,4), (1,3,2,4), (1,3,4) ]
1308 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, , 4 ] * 2;</span>
1309 [ 2, 4,, 8 ]
1310 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ] * [ 1, 3, 5, 7 ];</span>
1311 22
1312 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:= [ [ 1, 2 ], 3 ];; m * m;</span>
1313 [ [ 7, 8 ], [ [ 3, 6 ], 9 ] ]
1314 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m * m = [ m[1] * m, m[2] * m ];</span>
1315 true
1316 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">n:= [ 1, [ 2, 3 ] ];; n * n;</span>
1317 14
1318 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">n * n = n[1] * n[1] + n[2] * n[2];</span>
1319 true
1320 </pre></div>
1321
1322 <p><a id="X82EA2A5B786181C7" name="X82EA2A5B786181C7"></a></p>
1323
1324 <h5>21.14-4 <span class="Heading">Division of lists</span></h5>
1325
1326 <p>For two <strong class="pkg">GAP</strong> objects <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> of which one is in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) and the other is also in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) or is neither a list nor a domain, <span class="SimpleMath">x / y</span> is defined as <span class="SimpleMath">x * y^{-1}</span>.</p>
1327
1328
1329 <div class="example"><pre>
1330 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 1, 2, 3 ] / 2; [ 1, 2 ] / [ [ 1, 2 ], [ 3, 4 ] ];</span>
1331 [ 1/2, 1, 3/2 ]
1332 [ 1, 0 ]
1333 </pre></div>
1334
1335 <p><a id="X7A0FD70C80B95C00" name="X7A0FD70C80B95C00"></a></p>
1336
1337 <h5>21.14-5 <span class="Heading">mod for lists</span></h5>
1338
1339 <p>If <var class="Arg">x</var> and <var class="Arg">y</var> are in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) and have the same multiplicative nesting depth (see <code class="func">NestingDepthM</code> (<a href="chap21.html#X84B383B97FD986CD"><span class="RefLink">21.12-5</span></a>)), <code class="code"><var class="Arg">x</var> mod <var class="Arg">y</var></code> is defined <em>pointwise</em>, in the sense that the result is a list whose entry at position <span class="SimpleMath">i</span> is <code class="code"><var class="Arg">x</var>[i] mod <var class="Arg">y</var>[i]</code> if these entries are bound, is a shallow copy (see <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>)) of <span class="SimpleMath">x[i]</span> or <span class="SimpleMath">y[i]</span> if the other argument is not bound at position <span class="SimpleMath">i</span>, and is unbound if both <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> are unbound at position <span class="SimpleMath">i</span>.</p>
1340
1341 <p>If <span class="SimpleMath">x</span> is in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) and <span class="SimpleMath">y</span> is in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) and has lower multiplicative nesting depth or is neither a list nor a domain, <code class="code"><var class="Arg">x</var> mod <var class="Arg">y</var></code> is defined as a list whose entry at position <span class="SimpleMath">i</span> is <code class="code"><var class="Arg">x</var>[i] mod <var class="Arg">y</var></code> if <var class="Arg">x</var> is bound at position <span class="SimpleMath">i</span>, and is unbound if not. The equivalent holds in the reversed case, where the order of the arguments is kept.</p>
1342
1343
1344 <div class="example"><pre>
1345 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">4711 mod [ 2, 3,, 5, 7 ];</span>
1346 [ 1, 1,, 1, 0 ]
1347 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 2, 3, 4, 5, 6 ] mod 3;</span>
1348 [ 2, 0, 1, 2, 0 ]
1349 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">[ 10, 12, 14, 16 ] mod [ 3, 5, 7 ];</span>
1350 [ 1, 2, 0, 16 ]
1351 </pre></div>
1352
1353 <p><a id="X84BB2DFB8432A1A4" name="X84BB2DFB8432A1A4"></a></p>
1354
1355 <h5>21.14-6 <span class="Heading">Left quotients of lists</span></h5>
1356
1357 <p>For two <strong class="pkg">GAP</strong> objects <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> of which one is in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) and the other is also in <code class="func">IsMultiplicativeGeneralizedRowVector</code> (<a href="chap21.html#X7FBCA5B58308C158"><span class="RefLink">21.12-2</span></a>) or is neither a list nor a domain, <code class="code">LeftQuotient( <var class="Arg">x</var>, <var class="Arg">y</var> )</code> is defined as <span class="SimpleMath">x^{-1} * y</span>.</p>
1358
1359
1360 <div class="example"><pre>
1361 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LeftQuotient( [ [ 1, 2 ], [ 3, 4 ] ], [ 1, 2 ] );</span>
1362 [ 0, 1/2 ]
1363 </pre></div>
1364
1365 <p><a id="X8676EFE67972FD06" name="X8676EFE67972FD06"></a></p>
1366
1367 <h4>21.15 <span class="Heading">Mutability Status and List Arithmetic</span></h4>
1368
1369 <p>Many results of arithmetic operations, when applied to lists, are again lists, and it is of interest whether their entries are mutable or not (if applicable). Note that the mutability status of the result itself is already defined by the general rule for any result of an arithmetic operation, not only for lists (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>).</p>
1370
1371 <p>However, we do <em>not</em> define exactly the mutability status for each element on each level of a nested list returned by an arithmetic operation. (Of course it would be possible to define this recursively, but since the methods used are in general not recursive, in particular for efficient multiplication of compressed matrices, such a general definition would be a burden in these cases.) Instead we consider, for a list <span class="SimpleMath">x</span> in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>), the sequence <span class="SimpleMath">x = x_1, x_2, ... x_n</span> where <span class="SimpleMath">x_{i+1}</span> is the first bound entry in <span class="SimpleMath">x_i</span> if exists (that is, if <span class="SimpleMath">x_i</span> is a nonempty list), and <span class="SimpleMath">n</span> is the largest <span class="SimpleMath">i</span> such that <span class="SimpleMath">x_i</span> lies in <code class="func">IsGeneralizedRowVector</code> (<a href="chap21.html#X87ABCEE9809585A0"><span class="RefLink">21.12-1</span></a>). The <em>immutability level</em> of <span class="SimpleMath">x</span> is defined as infinity if <span class="SimpleMath">x</span> is immutable, and otherwise the number of <span class="SimpleMath">x_i</span> which are immutable. (So the immutability level of a mutable empty list is <span class="SimpleMath">0</span>.)</p>
1372
1373 <p>Thus a fully mutable matrix has immutability level <span class="SimpleMath">0</span>, and a mutable matrix with immutable first row has immutability level <span class="SimpleMath">1</span> (independent of the mutability of other rows).</p>
1374
1375 <p>The immutability level of the result of any of the binary operations discussed here is the minimum of the immutability levels of the arguments, provided that objects of the required mutability status exist in <strong class="pkg">GAP</strong>.</p>
1376
1377 <p>Moreover, the results have a <q>homogeneous</q> mutability status, that is, if the first bound entry at nesting depth <span class="SimpleMath">i</span> is immutable (mutable) then all entries at nesting depth <span class="SimpleMath">i</span> are immutable (mutable, provided that a mutable version of this entry exists in <strong class="pkg">GAP</strong>).</p>
1378
1379 <p>Thus the sum of two mutable matrices whose first rows are mutable is a matrix all of whose rows are mutable, and the product of two matrices whose first rows are immutable is a matrix all of whose rows are immutable, independent of the mutability status of the other rows of the arguments.</p>
1380
1381 <p>For example, the sum of a matrix (mutable or immutable, i.e., of immutability level one of <span class="SimpleMath">0</span>, <span class="SimpleMath">1</span>, or <span class="SimpleMath">2</span>) and a mutable row vector (i.e., immutability level <span class="SimpleMath">0</span>) is a fully mutable matrix. The product of two mutable row vectors of integers is an integer, and since <strong class="pkg">GAP</strong> does not support mutable integers, the result is immutable.</p>
1382
1383 <p>For unary arithmetic operations, there are three operations available, an attribute that returns an immutable result (<code class="func">Zero</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>), <code class="func">AdditiveInverse</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>), <code class="func">One</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>), <code class="func">Inverse</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>)), an operation that returns a result that is mutable (<code class="func">ZeroOp</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>), <code class="func">AdditiveInverseOp</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>), <code class="func">OneOp</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>), <code class="func">InverseOp</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>)), and an operation whose result has the same immutability level as the argument (<code class="func">ZeroSameMutability</code> (<a href="chap31.html#X8040AC7A79FFC442"><span class="RefLink">31.10-3</span></a>), <code class="func">AdditiveInverseSameMutability</code> (<a href="chap31.html#X84BB723C81D55D63"><span class="RefLink">31.10-9</span></a>), <code class="func">OneSameMutability</code> (<a href="chap31.html#X8046262384895B2A"><span class="RefLink">31.10-2</span></a>), <code class="func">InverseSameMutability</code> (<a href="chap31.html#X78EE524E83624057"><span class="RefLink">31.10-8</span></a>)). The last kind of operations is equivalent to the corresponding infix operations <code class="code">0 * <var class="Arg">list</var></code>, <code class="code">- <var class="Arg">list</var></code>, <code class="code"><var class="Arg">list</var>^0</code>, and <code class="code"><var class="Arg">list</var>^-1</code>. (This holds not only for lists, see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>.)</p>
1384
1385
1386 <div class="example"><pre>
1387 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l1 ); IsMutable( 2 * Immutable( [ 1, 2, 3 ] ) );</span>
1388 true
1389 false
1390 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l2 ); IsMutable( l3 );</span>
1391 true
1392 true
1393 </pre></div>
1394
1395 <p>An example motivating the mutability rule is the use of syntactic constructs such as <code class="code"><var class="Arg">obj</var> * <var class="Arg">list</var></code> and <code class="code">- <var class="Arg">list</var></code> as an elegant and efficient way to create mutable lists needed for further manipulations from mutable lists. In particular one can construct a mutable zero vector of length <span class="SimpleMath">n</span> by <code class="code">0 * [ 1 .. </code><span class="SimpleMath">n</span><code class="code"> ]</code>. The latter can be done also using <code class="func">ListWithIdenticalEntries</code> (<a href="chap21.html#X80FDB1457FF582E7"><span class="RefLink">21.15-1</span></a>).</p>
1396
1397 <p><a id="X80FDB1457FF582E7" name="X80FDB1457FF582E7"></a></p>
1398
1399 <h5>21.15-1 ListWithIdenticalEntries</h5>
1400
1401 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ListWithIdenticalEntries</code>( <var class="Arg">n</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1402 <p>is a list <var class="Arg">list</var> of length <var class="Arg">n</var> that has the object <var class="Arg">obj</var> stored at each of the positions from <code class="code">1</code> to <var class="Arg">n</var>. Note that all elements of <var class="Arg">lists</var> are identical, see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>.</p>
1403
1404
1405 <div class="example"><pre>
1406 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ListWithIdenticalEntries( 10, 0 );</span>
1407 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
1408 </pre></div>
1409
1410 <p><a id="X8196FD4779BCCA0C" name="X8196FD4779BCCA0C"></a></p>
1411
1412 <h4>21.16 <span class="Heading">Finding Positions in Lists</span></h4>
1413
1414 <p><a id="X79975EC6783B4293" name="X79975EC6783B4293"></a></p>
1415
1416 <h5>21.16-1 Position</h5>
1417
1418 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Position</code>( <var class="Arg">list</var>, <var class="Arg">obj</var>[, <var class="Arg">from</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1419 <p>returns the position of the first occurrence <var class="Arg">obj</var> in <var class="Arg">list</var>, or <code class="keyw">fail</code> if <var class="Arg">obj</var> is not contained in <var class="Arg">list</var>. If a starting index <var class="Arg">from</var> is given, it returns the position of the first occurrence starting the search <em>after</em> position <var class="Arg">from</var>.</p>
1420
1421 <p>Each call to the two argument version is translated into a call of the three argument version, with third argument the integer zero <code class="code">0</code>. (Methods for the two argument version must be installed as methods for the version with three arguments, the third being described by <code class="code">IsZeroCyc</code>.)</p>
1422
1423
1424 <div class="example"><pre>
1425 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position( [ 2, 2, 1, 3 ], 1 );</span>
1426 3
1427 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position( [ 2, 1, 1, 3 ], 1 );</span>
1428 2
1429 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position( [ 2, 1, 1, 3 ], 1, 2 );</span>
1430 3
1431 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position( [ 2, 1, 1, 3 ], 1, 3 );</span>
1432 fail
1433 </pre></div>
1434
1435 <p><a id="X7FA9648883AE1B88" name="X7FA9648883AE1B88"></a></p>
1436
1437 <h5>21.16-2 Positions</h5>
1438
1439 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Positions</code>( <var class="Arg">list</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1440 <p>returns the set of positions of <em>all</em> occurrences of <var class="Arg">obj</var> in <var class="Arg">list</var>.</p>
1441
1442 <p>Developers who wish to adapt this for custom list types need to install suitable methods for the operation <code class="code">PositionsOp</code>.</p>
1443
1444
1445 <div class="example"><pre>
1446 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Positions([1,2,1,2,3,2,2],2);</span>
1447 [ 2, 4, 6, 7 ]
1448 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Positions([1,2,1,2,3,2,2],4);</span>
1449 [ ]
1450 </pre></div>
1451
1452 <p><a id="X7B4B10AE81602D4E" name="X7B4B10AE81602D4E"></a></p>
1453
1454 <h5>21.16-3 PositionCanonical</h5>
1455
1456 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionCanonical</code>( <var class="Arg">list</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1457 <p>returns the position of the canonical associate of <var class="Arg">obj</var> in <var class="Arg">list</var>. The definition of this associate depends on <var class="Arg">list</var>. For internally represented lists it is defined as the element itself (and <code class="func">PositionCanonical</code> thus defaults to <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>), but for example for certain enumerators (see <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a>) other canonical associates can be defined.</p>
1458
1459 <p>For example <code class="func">RightTransversal</code> (<a href="chap39.html#X85C65D06822E716F"><span class="RefLink">39.8-1</span></a>) defines the canonical associate to be the element in the transversal defining the same coset of a subgroup in a group.</p>
1460
1461
1462 <div class="example"><pre>
1463 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">g:=Group((1,2,3,4),(1,2));;u:=Subgroup(g,[(1,2)(3,4),(1,3)(2,4)]);;</span>
1464 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">rt:=RightTransversal(g,u);;AsList(rt);</span>
1465 [ (), (3,4), (2,3), (2,3,4), (2,4,3), (2,4) ]
1466 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position(rt,(1,2));</span>
1467 fail
1468 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionCanonical(rt,(1,2));</span>
1469 2
1470 </pre></div>
1471
1472 <p><a id="X7D2B25B484591506" name="X7D2B25B484591506"></a></p>
1473
1474 <h5>21.16-4 PositionNthOccurrence</h5>
1475
1476 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionNthOccurrence</code>( <var class="Arg">list</var>, <var class="Arg">obj</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1477 <p>returns the position of the <var class="Arg">n</var>-th occurrence of <var class="Arg">obj</var> in <var class="Arg">list</var> and returns <code class="keyw">fail</code> if <var class="Arg">obj</var> does not occur <var class="Arg">n</var> times.</p>
1478
1479
1480 <div class="example"><pre>
1481 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNthOccurrence([1,2,3,2,4,2,1],1,1);</span>
1482 1
1483 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNthOccurrence([1,2,3,2,4,2,1],1,2);</span>
1484 7
1485 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNthOccurrence([1,2,3,2,4,2,1],2,3);</span>
1486 6
1487 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNthOccurrence([1,2,3,2,4,2,1],2,4);</span>
1488 fail
1489 </pre></div>
1490
1491 <p><a id="X7A122E848464E534" name="X7A122E848464E534"></a></p>
1492
1493 <h5>21.16-5 PositionSorted</h5>
1494
1495 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionSorted</code>( <var class="Arg">list</var>, <var class="Arg">elm</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1496 <p>Called with two arguments, <code class="func">PositionSorted</code> returns the position of the element <var class="Arg">elm</var> in the sorted list <var class="Arg">list</var>.</p>
1497
1498 <p>Called with three arguments, <code class="func">PositionSorted</code> returns the position of the element <var class="Arg">elm</var> in the list <var class="Arg">list</var>, which must be sorted with respect to <var class="Arg">func</var>. <var class="Arg">func</var> must be a function of two arguments that returns <code class="keyw">true</code> if the first argument is less than the second argument, and <code class="keyw">false</code> otherwise.</p>
1499
1500 <p><code class="func">PositionSorted</code> returns <var class="Arg">pos</var> such that <span class="SimpleMath"><var class="Arg">list</var>[<var class="Arg">pos</var>-1] &lt; <var class="Arg">elm</var> ≤ <var class="Arg">list</var>[<var class="Arg">pos</var>]</span> holds. That means, if <var class="Arg">elm</var> appears once in <var class="Arg">list</var>, its position is returned. If <var class="Arg">elm</var> appears several times in <var class="Arg">list</var>, the position of the first occurrence is returned. If <var class="Arg">elm</var> is not an element of <var class="Arg">list</var>, the index where <var class="Arg">elm</var> must be inserted to keep the list sorted is returned.</p>
1501
1502 <p><code class="func">PositionSorted</code> uses binary search, whereas <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) can in general use only linear search, see the remark at the beginning of <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>. For sorting lists, see <a href="chap21.html#X83E558E37D1B44D4"><span class="RefLink">21.18</span></a>, for testing whether a list is sorted, see <code class="func">IsSortedList</code> (<a href="chap21.html#X7BAA9B0E81D4A884"><span class="RefLink">21.17-3</span></a>) and <code class="func">IsSSortedList</code> (<a href="chap21.html#X80CDAF45782E8DCB"><span class="RefLink">21.17-4</span></a>).</p>
1503
1504 <p>Developers who wish to adapt this for custom list types need to install suitable methods for the operation <code class="code">PositionSortedOp</code>.</p>
1505
1506
1507 <div class="example"><pre>
1508 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSorted( [1,4,5,5,6,7], 0 );</span>
1509 1
1510 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSorted( [1,4,5,5,6,7], 2 );</span>
1511 2
1512 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSorted( [1,4,5,5,6,7], 4 );</span>
1513 2
1514 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSorted( [1,4,5,5,6,7], 5 );</span>
1515 3
1516 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSorted( [1,4,5,5,6,7], 8 );</span>
1517 7
1518 </pre></div>
1519
1520 <p><a id="X820BA44D85930EBF" name="X820BA44D85930EBF"></a></p>
1521
1522 <h5>21.16-6 PositionSortedBy</h5>
1523
1524 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionSortedBy</code>( <var class="Arg">list</var>, <var class="Arg">val</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1525 <p>This function returns the same value that would be returned by <code class="code">PositionSorted(List(list, func), val)</code>, but computes it in a more efficient way.</p>
1526
1527 <p>To be more precise, <var class="Arg">func</var> must be a function on one argument which returns values that can be compared to <var class="Arg">val</var>, and <var class="Arg">list</var> must be a list for which <code class="code">func(list[i]) &lt;= func(list[i+1])</code> holds for all relevant <var class="Arg">i</var>. This property is not verified, and if the input violates it, then the result is undefined.</p>
1528
1529 <p><code class="func">PositionSortedBy</code> returns <var class="Arg">pos</var> such that <span class="SimpleMath"><var class="Arg">func</var>(<var class="Arg">list</var>[<var class="Arg">pos</var>-1]) &lt; <var class="Arg">val</var> ≤ <var class="Arg">func</var>(<var class="Arg">list</var>[<var class="Arg">pos</var>])</span> holds. That means, if there are elements <code class="code">elm</code> in <var class="Arg">list</var> for which <span class="SimpleMath"><var class="Arg">func</var>(elm) = <var class="Arg">val</var></span> holds, then the position of the first such element is returned. If no element of <var class="Arg">list</var> satisfies this condition, then the lowest index where an element <var class="Arg">elm</var> satisfying <span class="SimpleMath"><var class="Arg">func</var>(elm) = <var class="Arg">val</var></span> must be inserted to preserve the property <code class="code">func(list[i]) &lt;= func(list[i+1])</code> is returned.</p>
1530
1531 <p><code class="func">PositionSortedBy</code> uses binary search. Each <code class="code">func(list[i])</code> is computed at most once.</p>
1532
1533 <p>Developers who wish to adapt this for custom list types need to install suitable methods for the operation <code class="code">PositionSortedByOp</code>.</p>
1534
1535
1536 <div class="example"><pre>
1537 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSortedBy( [ "", "ab", ], -1, Length );</span>
1538 1
1539 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSortedBy( [ "", "ab", ], 0, Length );</span>
1540 1
1541 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSortedBy( [ "", "ab", ], 1, Length );</span>
1542 2
1543 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSortedBy( [ "", "ab", ], 2, Length );</span>
1544 2
1545 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSortedBy( [ "", "ab", ], 3, Length );</span>
1546 3
1547 </pre></div>
1548
1549 <p><a id="X78BFE9D78347C0DA" name="X78BFE9D78347C0DA"></a></p>
1550
1551 <h5>21.16-7 PositionSet</h5>
1552
1553 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionSet</code>( <var class="Arg">list</var>, <var class="Arg">obj</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1554 <p><code class="func">PositionSet</code> is a slight variation of <code class="func">PositionSorted</code> (<a href="chap21.html#X7A122E848464E534"><span class="RefLink">21.16-5</span></a>). The only difference to <code class="func">PositionSorted</code> (<a href="chap21.html#X7A122E848464E534"><span class="RefLink">21.16-5</span></a>) is that <code class="func">PositionSet</code> returns <code class="keyw">fail</code> if <var class="Arg">obj</var> is not in <var class="Arg">list</var>.</p>
1555
1556
1557 <div class="example"><pre>
1558 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSet( [1,4,5,5,6,7], 0 );</span>
1559 fail
1560 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSet( [1,4,5,5,6,7], 2 );</span>
1561 fail
1562 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSet( [1,4,5,5,6,7], 4 );</span>
1563 2
1564 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSet( [1,4,5,5,6,7], 5 );</span>
1565 3
1566 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionSet( [1,4,5,5,6,7], 8 );</span>
1567 fail
1568 </pre></div>
1569
1570 <p><a id="X7FD9C1D37F300206" name="X7FD9C1D37F300206"></a></p>
1571
1572 <h5>21.16-8 PositionMaximum</h5>
1573
1574 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionMaximum</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1575 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionMinimum</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1576 <p>returns the position of maximum (with <code class="func">PositionMaximum</code>) or minimum (with <code class="func">PositionMinimum</code>) entry in the list <var class="Arg">list</var>. If a second argument <var class="Arg">func</var> is passed, then return instead the position of the largest/smallest entry in <code class="code">List( <var class="Arg">list</var> , <var class="Arg">func</var> )</code>. If several entries of the list are equal to the maximum/minimum, the first such position is returned.</p>
1577
1578
1579 <div class="example"><pre>
1580 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMaximum( [2,4,-6,2,4] );</span>
1581 2
1582 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMaximum( [2,4,-6,2,4], x -&gt; -x);</span>
1583 3
1584 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMinimum( [2,4,-6,2,4] );</span>
1585 3
1586 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionMinimum( [2,4,-6,2,4], x -&gt; -x);</span>
1587 2
1588 </pre></div>
1589
1590 <p><code class="func">Maximum</code> (<a href="chap21.html#X82CE0DE8828E4303"><span class="RefLink">21.20-12</span></a>) and <code class="func">Minimum</code> (<a href="chap21.html#X82F133EC7F89665F"><span class="RefLink">21.20-13</span></a>) allow you to find the maximum or minimum element of a list directly.</p>
1591
1592 <p><a id="X7E6C763A82C6153B" name="X7E6C763A82C6153B"></a></p>
1593
1594 <h5>21.16-9 PositionProperty</h5>
1595
1596 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionProperty</code>( <var class="Arg">list</var>, <var class="Arg">func</var>[, <var class="Arg">from</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1597 <p>returns the position of the first entry in the list <var class="Arg">list</var> for which the property tester function <var class="Arg">func</var> returns <code class="keyw">true</code>, or <code class="keyw">fail</code> if no such entry exists. If a starting index <var class="Arg">from</var> is given, it returns the position of the first entry satisfying <var class="Arg">func</var>, starting the search <em>after</em> position <var class="Arg">from</var>.</p>
1598
1599
1600 <div class="example"><pre>
1601 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionProperty( [10^7..10^8], IsPrime );</span>
1602 20
1603 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionProperty( [10^5..10^6],</span>
1604 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; not IsPrime(n) and IsPrimePowerInt(n) );</span>
1605 490
1606 </pre></div>
1607
1608 <p><code class="func">First</code> (<a href="chap21.html#X82801DFA84E11272"><span class="RefLink">21.20-21</span></a>) allows you to extract the first element of a list that satisfies a certain property.</p>
1609
1610 <p><a id="X7DA94D278304EC3D" name="X7DA94D278304EC3D"></a></p>
1611
1612 <h5>21.16-10 PositionsProperty</h5>
1613
1614 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionsProperty</code>( <var class="Arg">list</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1615 <p>returns the set of all those positions in the list <var class="Arg">list</var> which are bound and for which the property tester function <var class="Arg">func</var> returns <code class="keyw">true</code>.</p>
1616
1617
1618 <div class="example"><pre>
1619 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [ -5 .. 5 ];;</span>
1620 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionsProperty( l, IsPosInt );</span>
1621 [ 7, 8, 9, 10, 11 ]
1622 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionsProperty( l, IsPrimeInt );</span>
1623 [ 1, 3, 4, 8, 9, 11 ]
1624 </pre></div>
1625
1626 <p><code class="func">PositionProperty</code> (<a href="chap21.html#X7E6C763A82C6153B"><span class="RefLink">21.16-9</span></a>) allows you to extract the position of the first element in a list that satisfies a certain property.</p>
1627
1628 <p><a id="X86C9E5C3863B3C03" name="X86C9E5C3863B3C03"></a></p>
1629
1630 <h5>21.16-11 PositionBound</h5>
1631
1632 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionBound</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1633 <p>returns the first bound position of the list <var class="Arg">list</var>. For the empty list it returns <code class="keyw">fail</code>.</p>
1634
1635
1636 <div class="example"><pre>
1637 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionBound([1,2,3]);</span>
1638 1
1639 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionBound([,1,2,3]);</span>
1640 2
1641 </pre></div>
1642
1643 <p><a id="X819F71047AABEA2F" name="X819F71047AABEA2F"></a></p>
1644
1645 <h5>21.16-12 PositionsBound</h5>
1646
1647 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionsBound</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1648 <p>returns the set of all bound positions in the list <var class="Arg">list</var>.</p>
1649
1650
1651 <div class="example"><pre>
1652 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionsBound([1,2,3]);</span>
1653 [ 1 .. 3 ]
1654 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionsBound([,1,,3]);</span>
1655 [ 2, 4 ]
1656 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionsBound([]);</span>
1657 []
1658 </pre></div>
1659
1660 <p><a id="X865EF45D87ED1384" name="X865EF45D87ED1384"></a></p>
1661
1662 <h5>21.16-13 PositionNot</h5>
1663
1664 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionNot</code>( <var class="Arg">list</var>, <var class="Arg">val</var>[, <var class="Arg">from</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1665 <p>For a list <var class="Arg">list</var> and an object <var class="Arg">val</var>, <code class="func">PositionNot</code> returns the smallest nonnegative integer <span class="SimpleMath">n</span> such that <span class="SimpleMath"><var class="Arg">list</var>[n]</span> is either unbound or not equal to <var class="Arg">val</var>. If a starting index <var class="Arg">from</var> is given, it returns the first position with this property starting the search <em>after</em> position <var class="Arg">from</var>.</p>
1666
1667
1668 <div class="example"><pre>
1669 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [ 1, 1, 2, 3, 2 ];; PositionNot( l, 1 );</span>
1670 3
1671 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNot( l, 1, 4 ); PositionNot( l, 2, 4 );</span>
1672 5
1673 6
1674 </pre></div>
1675
1676 <p><a id="X7F42E5AD87EC9D5A" name="X7F42E5AD87EC9D5A"></a></p>
1677
1678 <h5>21.16-14 PositionNonZero</h5>
1679
1680 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionNonZero</code>( <var class="Arg">vec</var>[, <var class="Arg">from</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1681 <p>For a row vector <var class="Arg">vec</var>, <code class="func">PositionNonZero</code> returns the position of the first non-zero element of <var class="Arg">vec</var>, or <code class="code">Length(</code> <var class="Arg">vec</var> <code class="code">)+1</code> if all entries of <var class="Arg">vec</var> are zero.</p>
1682
1683 <p>If a starting index <var class="Arg">from</var> is given, it returns the position of the first occurrence starting the search <em>after</em> position <var class="Arg">from</var>.</p>
1684
1685 <p><code class="func">PositionNonZero</code> implements a special case of <code class="func">PositionNot</code> (<a href="chap21.html#X865EF45D87ED1384"><span class="RefLink">21.16-13</span></a>). Namely, the element to be avoided is the zero element, and the list must be (at least) homogeneous because otherwise the zero element cannot be specified implicitly.</p>
1686
1687
1688 <div class="example"><pre>
1689 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNonZero( [ 1, 1, 2, 3, 2 ] );</span>
1690 1
1691 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PositionNonZero( [ 2, 3, 4, 5 ] * Z(2) );</span>
1692 2
1693 </pre></div>
1694
1695 <p><a id="X87A8C62A867D6DA4" name="X87A8C62A867D6DA4"></a></p>
1696
1697 <h5>21.16-15 PositionSublist</h5>
1698
1699 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositionSublist</code>( <var class="Arg">list</var>, <var class="Arg">sub</var>[, <var class="Arg">from</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1700 <p>returns the smallest index in the list <var class="Arg">list</var> at which a sublist equal to <var class="Arg">sub</var> starts. If <var class="Arg">sub</var> does not occur the operation returns <code class="keyw">fail</code>. The version with given <var class="Arg">from</var> starts searching <em>after</em> position <var class="Arg">from</var>.</p>
1701
1702 <p>To determine whether <var class="Arg">sub</var> matches <var class="Arg">list</var> at a particular position, use <code class="func">IsMatchingSublist</code> (<a href="chap21.html#X83F8EC7C7BF27EFC"><span class="RefLink">21.17-1</span></a>) instead.</p>
1703
1704 <p><a id="X7865747A7CCF5812" name="X7865747A7CCF5812"></a></p>
1705
1706 <h4>21.17 <span class="Heading">Properties and Attributes for Lists</span></h4>
1707
1708 <p>A list that contains mutable objects (like lists or records) <em>cannot</em> store attribute values that depend on the values of its entries, such as whether it is homogeneous, sorted, or strictly sorted, as changes in any of its entries could change such property values, like the following example shows.</p>
1709
1710
1711 <div class="example"><pre>
1712 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=[[1],[2]];</span>
1713 [ [ 1 ], [ 2 ] ]
1714 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSSortedList(l);</span>
1715 true
1716 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[1][1]:=3;</span>
1717 3
1718 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSSortedList(l);</span>
1719 false
1720 </pre></div>
1721
1722 <p>For such lists these property values must be computed anew each time the property is asked for. For example, if <var class="Arg">list</var> is a list of mutable row vectors then the call of <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) with <var class="Arg">list</var> as first argument cannot take advantage of the fact that <var class="Arg">list</var> is in fact sorted. One solution is to call explicitly <code class="func">PositionSorted</code> (<a href="chap21.html#X7A122E848464E534"><span class="RefLink">21.16-5</span></a>) in such a situation, another solution is to replace <var class="Arg">list</var> by an immutable copy using <code class="func">Immutable</code> (<a href="chap12.html#X7F0ABF2C870B0CBB"><span class="RefLink">12.6-3</span></a>).</p>
1723
1724 <p><a id="X83F8EC7C7BF27EFC" name="X83F8EC7C7BF27EFC"></a></p>
1725
1726 <h5>21.17-1 IsMatchingSublist</h5>
1727
1728 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsMatchingSublist</code>( <var class="Arg">list</var>, <var class="Arg">sub</var>[, <var class="Arg">at</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1729 <p>returns <code class="keyw">true</code> if <var class="Arg">sub</var> matches a sublist of <var class="Arg">list</var> from position <code class="code">1</code> (or position <var class="Arg">at</var>, in the case of three arguments), or <code class="keyw">false</code>, otherwise. If <var class="Arg">sub</var> is empty <code class="keyw">true</code> is returned. If <var class="Arg">list</var> is empty but <var class="Arg">sub</var> is non-empty <code class="keyw">false</code> is returned.</p>
1730
1731 <p>If you actually want to know whether there is an <var class="Arg">at</var> for which <code class="code">IsMatchingSublist( <var class="Arg">list</var>, <var class="Arg">sub</var>, <var class="Arg">at</var> )</code> is true, use a construction like <code class="code">PositionSublist( <var class="Arg">list</var>, <var class="Arg">sub</var> ) &lt;&gt; fail</code> instead (see <code class="func">PositionSublist</code> (<a href="chap21.html#X87A8C62A867D6DA4"><span class="RefLink">21.16-15</span></a>)); it's more efficient.</p>
1732
1733 <p><a id="X7FA892828252BB3B" name="X7FA892828252BB3B"></a></p>
1734
1735 <h5>21.17-2 IsDuplicateFree</h5>
1736
1737 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsDuplicateFree</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
1738 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsDuplicateFreeList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;filter&nbsp;)</td></tr></table></div>
1739 <p><code class="func">IsDuplicateFree</code> returns <code class="keyw">true</code> if <var class="Arg">obj</var> is both a list or collection, and it is duplicate free; otherwise it returns <code class="keyw">false</code>. <code class="func">IsDuplicateFreeList</code> is a synonym for <code class="code">IsDuplicateFree and IsList</code>.</p>
1740
1741 <p>A list is <em>duplicate free</em> if it is dense and does not contain equal entries in different positions. Every domain (see <a href="chap12.html#X7BAF69417BB925F6"><span class="RefLink">12.4</span></a>) is duplicate free.</p>
1742
1743 <p>Note that <strong class="pkg">GAP</strong> cannot compare arbitrary objects (by equality). This can cause that <code class="func">IsDuplicateFree</code> runs into an error, if <var class="Arg">obj</var> is a list with some non-comparable entries.</p>
1744
1745 <p><a id="X7BAA9B0E81D4A884" name="X7BAA9B0E81D4A884"></a></p>
1746
1747 <h5>21.17-3 IsSortedList</h5>
1748
1749 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSortedList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
1750 <p>returns <code class="keyw">true</code> if <var class="Arg">obj</var> is a list and it is sorted, and <code class="keyw">false</code> otherwise.</p>
1751
1752 <p>A list <var class="Arg">list</var> is <em>sorted</em> if it is dense (see <code class="func">IsDenseList</code> (<a href="chap21.html#X870AA9D8798C93DD"><span class="RefLink">21.1-2</span></a>)) and satisfies the relation <span class="SimpleMath"><var class="Arg">list</var>[i] ≤ <var class="Arg">list</var>[j]</span> whenever <span class="SimpleMath">i &lt; j</span>. Note that a sorted list is not necessarily duplicate free (see <code class="func">IsDuplicateFree</code> (<a href="chap21.html#X7FA892828252BB3B"><span class="RefLink">21.17-2</span></a>) and <code class="func">IsSSortedList</code> (<a href="chap21.html#X80CDAF45782E8DCB"><span class="RefLink">21.17-4</span></a>)).</p>
1753
1754 <p>Many sorted lists are in fact homogeneous (see <code class="func">IsHomogeneousList</code> (<a href="chap21.html#X7C71596C82B6EF35"><span class="RefLink">21.1-3</span></a>)), but also non-homogeneous lists may be sorted (see <a href="chap31.html#X7B3BC7BA7BB2646D"><span class="RefLink">31.11</span></a>).</p>
1755
1756 <p>In sorted lists, membership test and computing of positions can be done by binary search, see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>.</p>
1757
1758 <p>Note that <strong class="pkg">GAP</strong> cannot compare (by less than) arbitrary objects. This can cause that <code class="func">IsSortedList</code> runs into an error, if <var class="Arg">obj</var> is a list with some non-comparable entries.</p>
1759
1760 <p><a id="X80CDAF45782E8DCB" name="X80CDAF45782E8DCB"></a></p>
1761
1762 <h5>21.17-4 IsSSortedList</h5>
1763
1764 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSSortedList</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
1765 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSet</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
1766 <p>returns <code class="keyw">true</code> if <var class="Arg">obj</var> is a list and it is strictly sorted, and <code class="keyw">false</code> otherwise. <code class="func">IsSSortedList</code> is short for <q>is strictly sorted list</q>; <code class="func">IsSet</code> is just a synonym for <code class="func">IsSSortedList</code>.</p>
1767
1768 <p>A list <var class="Arg">list</var> is <em>strictly sorted</em> if it is sorted (see <code class="func">IsSortedList</code> (<a href="chap21.html#X7BAA9B0E81D4A884"><span class="RefLink">21.17-3</span></a>)) and satisfies the relation <span class="SimpleMath"><var class="Arg">list</var>[i] &lt; <var class="Arg">list</var>[j]</span> whenever <span class="SimpleMath">i &lt; j</span>. In particular, such lists are duplicate free (see <code class="func">IsDuplicateFree</code> (<a href="chap21.html#X7FA892828252BB3B"><span class="RefLink">21.17-2</span></a>)).</p>
1769
1770 <p>(Currently there is little special treatment of lists that are sorted but not strictly sorted. In particular, internally represented lists will <em>not</em> store that they are sorted but not strictly sorted.)</p>
1771
1772 <p>Note that <strong class="pkg">GAP</strong> cannot compare (by less than) arbitrary objects. This can cause that <code class="func">IsSSortedList</code> runs into an error, if <var class="Arg">obj</var> is a list with some non-comparable entries.</p>
1773
1774 <p><a id="X780769238600AFD1" name="X780769238600AFD1"></a></p>
1775
1776 <h5>21.17-5 Length</h5>
1777
1778 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Length</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
1779 <p>returns the <em>length</em> of the list <var class="Arg">list</var>, which is defined to be the index of the last bound entry in <var class="Arg">list</var>.</p>
1780
1781 <p><a id="X7B55FB967CDEF468" name="X7B55FB967CDEF468"></a></p>
1782
1783 <h5>21.17-6 ConstantTimeAccessList</h5>
1784
1785 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ConstantTimeAccessList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
1786 <p><code class="func">ConstantTimeAccessList</code> returns an immutable list containing the same elements as the list <var class="Arg">list</var> (which may have holes) in the same order. If <var class="Arg">list</var> is already a constant time access list, <code class="func">ConstantTimeAccessList</code> returns an immutable copy of <var class="Arg">list</var> directly. Otherwise it puts all elements and holes of <var class="Arg">list</var> into a new list and makes that list immutable.</p>
1787
1788 <p><a id="X83E558E37D1B44D4" name="X83E558E37D1B44D4"></a></p>
1789
1790 <h4>21.18 <span class="Heading">Sorting Lists</span></h4>
1791
1792 <p>GAP implements three different families of sorting algorithms. The default algorithm is pattern-defeating quicksort, a variant of quicksort which performs better on partially sorted lists and has good worst-case behaviour. The functions which begin <code class="code">Stable</code> are stable (equal elements keep the same relative order in the sorted list) and use merge sort. Finally, the functions which begin <code class="code">Shell</code> use the shell sort which was GAP's default search algorithm before 4.9. <code class="func">Sortex</code> (<a href="chap21.html#X87287FCA81E2B06A"><span class="RefLink">21.18-3</span></a>) and <code class="func">SortingPerm</code> (<a href="chap21.html#X800209E881E7CECB"><span class="RefLink">21.18-4</span></a>) are also stable.</p>
1793
1794 <p><a id="X7FE4975F8166884D" name="X7FE4975F8166884D"></a></p>
1795
1796 <h5>21.18-1 Sort</h5>
1797
1798 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Sort</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1799 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SortBy</code>( <var class="Arg">list</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1800 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StableSort</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1801 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StableSortBy</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1802 <p><code class="func">Sort</code> sorts the list <var class="Arg">list</var> in increasing order. In the one argument form <code class="func">Sort</code> uses the operator <code class="code">&lt;</code> to compare the elements. (If the list is not homogeneous it is the user's responsibility to ensure that <code class="code">&lt;</code> is defined for all element pairs, see <a href="chap31.html#X7B3BC7BA7BB2646D"><span class="RefLink">31.11</span></a>) In the two argument form <code class="func">Sort</code> uses the function <var class="Arg">func</var> to compare elements. <var class="Arg">func</var> must be a function taking two arguments that returns <code class="keyw">true</code> if the first is regarded as strictly smaller than the second, and <code class="keyw">false</code> otherwise.</p>
1803
1804 <p><code class="func">StableSort</code> behaves identically to <code class="func">Sort</code>, except that <code class="func">StableSort</code> will keep elements which compare equal in the same relative order, while <code class="func">Sort</code> may change their relative order.</p>
1805
1806 <p><code class="func">Sort</code> does not return anything, it just changes the argument <var class="Arg">list</var>. Use <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) if you want to keep <var class="Arg">list</var>. Use <code class="func">Reversed</code> (<a href="chap21.html#X7C4FDB007C3F54A1"><span class="RefLink">21.20-7</span></a>) if you want to get a new list that is sorted in decreasing order.</p>
1807
1808 <p><code class="func">SortBy</code> sorts the list <var class="Arg">list</var> into an order such that <code class="code">func(list[i]) &lt;= func(list[i+1])</code> for all relevant <var class="Arg">i</var>. <var class="Arg">func</var> must thus be a function on one argument which returns values that can be compared. Each <code class="code">func(list[i])</code> is computed just once and stored, making this more efficient than using the two-argument version of <code class="func">Sort</code> in many cases.</p>
1809
1810 <p><code class="func">StableSortBy</code> behaves the same as <code class="func">SortBy</code> except that, like <code class="func">StableSort</code>, it keeps pairs of values which compare equal when <code class="code">func</code> is applied to them in the same relative order.</p>
1811
1812
1813 <div class="example"><pre>
1814 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list := [ 5, 4, 6, 1, 7, 5 ];; Sort( list ); list;</span>
1815 [ 1, 4, 5, 5, 6, 7 ]
1816 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortBy(list, x -&gt; x mod 3);</span>
1817 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list; # Sorted by mod 3</span>
1818 [ 6, 1, 4, 7, 5, 5]
1819 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list := [ [0,6], [1,2], [1,3], [1,5], [0,4], [3,4] ];;</span>
1820 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sort( list, function(v,w) return v*v &lt; w*w; end );</span>
1821 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list; # sorted according to the Euclidean distance from [0,0]</span>
1822 [ [ 1, 2 ], [ 1, 3 ], [ 0, 4 ], [ 3, 4 ], [ 1, 5 ], [ 0, 6 ] ]
1823 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortBy( list, function(v) return v[1] + v[2]; end );</span>
1824 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list; # sorted according to Manhattan distance from [0,0]</span>
1825 [ [ 1, 2 ], [ 1, 3 ], [ 0, 4 ], [ 1, 5 ], [ 0, 6 ], [ 3, 4 ] ]
1826 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list := [ [0,6], [1,3], [3,4], [1,5], [1,2], [0,4], ];;</span>
1827 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sort( list, function(v,w) return v[1] &lt; w[1]; end );</span>
1828 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># note the random order of the elements with equal first component:</span>
1829 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list;</span>
1830 [ [ 0, 6 ], [ 0, 4 ], [ 1, 3 ], [ 1, 5 ], [ 1, 2 ], [ 3, 4 ] ]
1831 </pre></div>
1832
1833 <p><a id="X791F2B2C7E9B9A46" name="X791F2B2C7E9B9A46"></a></p>
1834
1835 <h5>21.18-2 SortParallel</h5>
1836
1837 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SortParallel</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1838 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; StableSortParallel</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1839 <p><code class="func">SortParallel</code> sorts the list <var class="Arg">list1</var> in increasing order just as <code class="func">Sort</code> (<a href="chap21.html#X7FE4975F8166884D"><span class="RefLink">21.18-1</span></a>) does. In parallel it applies the same exchanges that are necessary to sort <var class="Arg">list1</var> to the list <var class="Arg">list2</var>, which must of course have at least as many elements as <var class="Arg">list1</var> does.</p>
1840
1841 <p><code class="func">StableSortParallel</code> behaves identically to <code class="func">SortParallel</code>, except it keeps elements in <var class="Arg">list1</var> which compare equal in the same relative order.</p>
1842
1843
1844 <div class="example"><pre>
1845 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ 5, 4, 6, 1, 7, 5 ];;</span>
1846 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2 := [ 2, 3, 5, 7, 8, 9 ];;</span>
1847 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortParallel( list1, list2 );</span>
1848 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1;</span>
1849 [ 1, 4, 5, 5, 6, 7 ]
1850 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2;</span>
1851 [ 7, 3, 2, 9, 5, 8 ]
1852 </pre></div>
1853
1854 <p>Note that <code class="code">[ 7, 3, 2, 9, 5, 8 ]</code> or <code class="code">[ 7, 3, 9, 2, 5, 8 ]</code> are possible results. <code class="func">StableSortParallel</code> will always return <code class="code">[ 7, 3, 2, 9, 5, 8]</code>.</p>
1855
1856 <p><a id="X87287FCA81E2B06A" name="X87287FCA81E2B06A"></a></p>
1857
1858 <h5>21.18-3 Sortex</h5>
1859
1860 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Sortex</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1861 <p>sorts the list <var class="Arg">list</var> and returns a permutation that can be applied to <var class="Arg">list</var> to obtain the sorted list. The one argument form sorts via the operator <code class="code">&lt;</code>, the two argument form sorts w.r.t. the function <var class="Arg">func</var>. The permutation returned by <code class="func">Sortex</code> will keep elements which compare equal in the same relative order. (If the list is not homogeneous it is the user's responsibility to ensure that <code class="code">&lt;</code> is defined for all element pairs, see <a href="chap31.html#X7B3BC7BA7BB2646D"><span class="RefLink">31.11</span></a>)</p>
1862
1863 <p><code class="func">Permuted</code> (<a href="chap21.html#X7B5A19098406347A"><span class="RefLink">21.20-17</span></a>) allows you to rearrange a list according to a given permutation.</p>
1864
1865
1866 <div class="example"><pre>
1867 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ 5, 4, 6, 1, 7, 5 ];;</span>
1868 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2 := ShallowCopy( list1 );;</span>
1869 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">perm := Sortex( list1 );</span>
1870 (1,3,5,6,4)
1871 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1;</span>
1872 [ 1, 4, 5, 5, 6, 7 ]
1873 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Permuted( list2, perm );</span>
1874 [ 1, 4, 5, 5, 6, 7 ]
1875 </pre></div>
1876
1877 <p><a id="X800209E881E7CECB" name="X800209E881E7CECB"></a></p>
1878
1879 <h5>21.18-4 SortingPerm</h5>
1880
1881 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SortingPerm</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
1882 <p><code class="func">SortingPerm</code> returns the same as <code class="func">Sortex</code> (<a href="chap21.html#X87287FCA81E2B06A"><span class="RefLink">21.18-3</span></a>) but does <em>not</em> change the argument.</p>
1883
1884
1885 <div class="example"><pre>
1886 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ 5, 4, 6, 1, 7, 5 ];;</span>
1887 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2 := ShallowCopy( list1 );;</span>
1888 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">perm := SortingPerm( list1 );</span>
1889 (1,3,5,6,4)
1890 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1;</span>
1891 [ 5, 4, 6, 1, 7, 5 ]
1892 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Permuted( list2, perm );</span>
1893 [ 1, 4, 5, 5, 6, 7 ]
1894 </pre></div>
1895
1896 <p><a id="X80ABC25582343910" name="X80ABC25582343910"></a></p>
1897
1898 <h4>21.19 <span class="Heading">Sorted Lists and Sets</span></h4>
1899
1900 <p>Searching objects in a list works much quicker if the list is known to be sorted. Currently <strong class="pkg">GAP</strong> exploits the sortedness of a list automatically only if the list is <em>strictly sorted</em>, which is indicated by the property <code class="func">IsSSortedList</code> (<a href="chap21.html#X80CDAF45782E8DCB"><span class="RefLink">21.17-4</span></a>).</p>
1901
1902 <p>Remember that a list of <em>mutable</em> objects cannot store that it is strictly sorted but has to test it anew whenever it is asked whether it is sorted, see the remark in <a href="chap21.html#X7865747A7CCF5812"><span class="RefLink">21.17</span></a>. Therefore <strong class="pkg">GAP</strong> cannot take advantage of the sortedness of a list if this list has mutable entries. Moreover, if a sorted list <var class="Arg">list</var> with mutable elements is used as an argument of a function that <em>expects</em> this argument to be sorted, for example <code class="func">UniteSet</code> (<a href="chap21.html#X7B3469CD7EFC1A87"><span class="RefLink">21.19-6</span></a>) or <code class="func">RemoveSet</code> (<a href="chap21.html#X7FCA282E789A4F4B"><span class="RefLink">21.19-5</span></a>), then it is checked whether <var class="Arg">list</var> is in fact sorted; this check can have the effect actually to slow down the computations, compared to computations with sorted lists of immutable elements or computations that do not involve functions that do automatically check sortedness.</p>
1903
1904 <p>Strictly sorted lists are used to represent <em>sets</em> in <strong class="pkg">GAP</strong>. More precisely, a strictly sorted list is called a <em>proper set</em> in the following, in order to avoid confusion with domains (see <a href="chap12.html#X7BAF69417BB925F6"><span class="RefLink">12.4</span></a>) which also represent sets.</p>
1905
1906 <p>In short proper sets are represented by sorted lists without holes and duplicates in <strong class="pkg">GAP</strong>. Note that we guarantee this representation, so you may make use of the fact that a set is represented by a sorted list in your functions.</p>
1907
1908 <p>In some contexts (for example see <a href="chap16.html#X7BDA99EE7CEADA7C"><span class="RefLink">16</span></a>), we also want to talk about multisets. A <em>multiset</em> is like a set, except that an element may appear several times in a multiset. Such multisets are represented by sorted lists without holes that may have duplicates.</p>
1909
1910 <p>This section lists only those functions that are defined exclusively for proper sets. Set theoretic functions for general collections, such as <code class="func">Intersection</code> (<a href="chap30.html#X851069107CACF98E"><span class="RefLink">30.5-2</span></a>) and <code class="func">Union</code> (<a href="chap30.html#X799F0E2F7A502DBA"><span class="RefLink">30.5-3</span></a>), are described in Chapter <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>. In particular, for the construction of proper sets, see <code class="func">SSortedList</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) and <code class="func">AsSSortedList</code> (<a href="chap30.html#X856D927378C33548"><span class="RefLink">30.3-10</span></a>). For finding positions in sorted lists, see <code class="func">PositionSorted</code> (<a href="chap21.html#X7A122E848464E534"><span class="RefLink">21.16-5</span></a>).</p>
1911
1912 <p>There are nondestructive counterparts of the functions <code class="func">UniteSet</code> (<a href="chap21.html#X7B3469CD7EFC1A87"><span class="RefLink">21.19-6</span></a>), <code class="func">IntersectSet</code> (<a href="chap21.html#X8473AA657FEC3D4D"><span class="RefLink">21.19-7</span></a>), and <code class="func">SubtractSet</code> (<a href="chap21.html#X80B427537EB07D09"><span class="RefLink">21.19-8</span></a>) available for proper sets. These are <code class="code">UnionSet</code>, <code class="code">IntersectionSet</code>, and <code class="func">Difference</code> (<a href="chap30.html#X825AC0F07E010B07"><span class="RefLink">30.5-4</span></a>). The former two are methods for the more general operations <code class="func">Union</code> (<a href="chap30.html#X799F0E2F7A502DBA"><span class="RefLink">30.5-3</span></a>) and <code class="func">Intersection</code> (<a href="chap30.html#X851069107CACF98E"><span class="RefLink">30.5-2</span></a>), the latter is itself an operation (see <code class="func">Difference</code> (<a href="chap30.html#X825AC0F07E010B07"><span class="RefLink">30.5-4</span></a>)).</p>
1913
1914 <p>The result of <code class="code">IntersectionSet</code> and <code class="code">UnionSet</code> is always a new list, that is not identical to any other list. The elements of that list however are identical to the corresponding elements of the first argument <var class="Arg">set</var>. If <var class="Arg">set</var> is not a proper set it is not specified to which of a number of equal elements in <var class="Arg">set</var> the element in the result is identical (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>). The following functions, if not explicitly stated differently, take two arguments, <var class="Arg">set</var> and <var class="Arg">obj</var>, where <var class="Arg">set</var> must be a proper set, otherwise an error is signalled; If the second argument <var class="Arg">obj</var> is a list that is not a proper set then <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) is silently applied to it first.</p>
1915
1916 <p><a id="X7B16AD597CB12305" name="X7B16AD597CB12305"></a></p>
1917
1918 <h5><code>21.19-1 \in</code></h5>
1919
1920 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \in</code>( <var class="Arg">obj</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;method&nbsp;)</td></tr></table></div>
1921 <p>For a list <var class="Arg">list</var> that stores that it is strictly sorted, the test with <code class="func">\in</code> whether the object <var class="Arg">obj</var> is an entry of <var class="Arg">list</var> uses binary search. This test can be entered also with the infix notation <var class="Arg">obj</var> <code class="keyw">in</code> <var class="Arg">list</var>.</p>
1922
1923 <p><a id="X7B4C0FEE7CDF6F2A" name="X7B4C0FEE7CDF6F2A"></a></p>
1924
1925 <h5>21.19-2 IsEqualSet</h5>
1926
1927 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsEqualSet</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1928 <p>tests whether <var class="Arg">list1</var> and <var class="Arg">list2</var> are equal <em>when viewed as sets</em>, that is if every element of <var class="Arg">list1</var> is an element of <var class="Arg">list2</var> and vice versa. Either argument of <code class="func">IsEqualSet</code> may also be a list that is not a proper set, in which case <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) is applied to it first.</p>
1929
1930 <p>If both lists are proper sets then they are of course equal if and only if they are also equal as lists. Thus <code class="code">IsEqualSet( <var class="Arg">list1</var>, <var class="Arg">list2</var> )</code> is equivalent to <code class="code">Set( <var class="Arg">list1</var> ) = Set( <var class="Arg">list2</var> )</code> (see <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>)), but the former is more efficient.</p>
1931
1932
1933 <div class="example"><pre>
1934 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsEqualSet( [2,3,5,7,11], [11,7,5,3,2] );</span>
1935 true
1936 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsEqualSet( [2,3,5,7,11], [2,3,5,7,11,13] );</span>
1937 false
1938 </pre></div>
1939
1940 <p><a id="X79B940567A849216" name="X79B940567A849216"></a></p>
1941
1942 <h5>21.19-3 IsSubsetSet</h5>
1943
1944 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSubsetSet</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1945 <p>tests whether every element of <var class="Arg">list2</var> is contained in <var class="Arg">list1</var>. Either argument of <code class="func">IsSubsetSet</code> may also be a list that is not a proper set, in which case <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) is applied to it first.</p>
1946
1947 <p><a id="X832C23CC7FCD8892" name="X832C23CC7FCD8892"></a></p>
1948
1949 <h5>21.19-4 AddSet</h5>
1950
1951 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AddSet</code>( <var class="Arg">set</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1952 <p>adds the element <var class="Arg">obj</var> to the proper set <var class="Arg">set</var>. If <var class="Arg">obj</var> is already contained in <var class="Arg">set</var> then <var class="Arg">set</var> is not changed. Otherwise <var class="Arg">obj</var> is inserted at the correct position such that <var class="Arg">set</var> is again a proper set afterwards.</p>
1953
1954 <p>Note that <var class="Arg">obj</var> must be in the same family as each element of <var class="Arg">set</var>.</p>
1955
1956
1957 <div class="example"><pre>
1958 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := [2,3,7,11];;</span>
1959 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AddSet( s, 5 ); s;</span>
1960 [ 2, 3, 5, 7, 11 ]
1961 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AddSet( s, 13 ); s;</span>
1962 [ 2, 3, 5, 7, 11, 13 ]
1963 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AddSet( s, 3 ); s;</span>
1964 [ 2, 3, 5, 7, 11, 13 ]
1965 </pre></div>
1966
1967 <p><a id="X7FCA282E789A4F4B" name="X7FCA282E789A4F4B"></a></p>
1968
1969 <h5>21.19-5 RemoveSet</h5>
1970
1971 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RemoveSet</code>( <var class="Arg">set</var>, <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1972 <p>removes the element <var class="Arg">obj</var> from the proper set <var class="Arg">set</var>. If <var class="Arg">obj</var> is not contained in <var class="Arg">set</var> then <var class="Arg">set</var> is not changed. If <var class="Arg">obj</var> is an element of <var class="Arg">set</var> it is removed and all the following elements in the list are moved one position forward.</p>
1973
1974
1975 <div class="example"><pre>
1976 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := [ 2, 3, 4, 5, 6, 7 ];;</span>
1977 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RemoveSet( s, 6 ); s;</span>
1978 [ 2, 3, 4, 5, 7 ]
1979 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RemoveSet( s, 10 ); s;</span>
1980 [ 2, 3, 4, 5, 7 ]
1981 </pre></div>
1982
1983 <p><a id="X7B3469CD7EFC1A87" name="X7B3469CD7EFC1A87"></a></p>
1984
1985 <h5>21.19-6 UniteSet</h5>
1986
1987 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; UniteSet</code>( <var class="Arg">set</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1988 <p>unites the proper set <var class="Arg">set</var> with <var class="Arg">list</var>. This is equivalent to adding all elements of <var class="Arg">list</var> to <var class="Arg">set</var> (see <code class="func">AddSet</code> (<a href="chap21.html#X832C23CC7FCD8892"><span class="RefLink">21.19-4</span></a>)).</p>
1989
1990
1991 <div class="example"><pre>
1992 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">set := [ 2, 3, 5, 7, 11 ];;</span>
1993 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">UniteSet( set, [ 4, 8, 9 ] ); set;</span>
1994 [ 2, 3, 4, 5, 7, 8, 9, 11 ]
1995 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">UniteSet( set, [ 16, 9, 25, 13, 16 ] ); set;</span>
1996 [ 2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 25 ]
1997 </pre></div>
1998
1999 <p><a id="X8473AA657FEC3D4D" name="X8473AA657FEC3D4D"></a></p>
2000
2001 <h5>21.19-7 IntersectSet</h5>
2002
2003 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IntersectSet</code>( <var class="Arg">set</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2004 <p>intersects the proper set <var class="Arg">set</var> with <var class="Arg">list</var>. This is equivalent to removing from <var class="Arg">set</var> all elements of <var class="Arg">set</var> that are not contained in <var class="Arg">list</var>.</p>
2005
2006
2007 <div class="example"><pre>
2008 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">set := [ 2, 3, 4, 5, 7, 8, 9, 11, 13, 16 ];;</span>
2009 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IntersectSet( set, [ 3, 5, 7, 9, 11, 13, 15, 17 ] ); set;</span>
2010 [ 3, 5, 7, 9, 11, 13 ]
2011 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IntersectSet( set, [ 9, 4, 6, 8 ] ); set;</span>
2012 [ 9 ]
2013 </pre></div>
2014
2015 <p><a id="X80B427537EB07D09" name="X80B427537EB07D09"></a></p>
2016
2017 <h5>21.19-8 SubtractSet</h5>
2018
2019 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SubtractSet</code>( <var class="Arg">set</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2020 <p>subtracts <var class="Arg">list</var> from the proper set <var class="Arg">set</var>. This is equivalent to removing from <var class="Arg">set</var> all elements of <var class="Arg">list</var>.</p>
2021
2022
2023 <div class="example"><pre>
2024 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">set := [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];;</span>
2025 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SubtractSet( set, [ 6, 10 ] ); set;</span>
2026 [ 2, 3, 4, 5, 7, 8, 9, 11 ]
2027 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SubtractSet( set, [ 9, 4, 6, 8 ] ); set;</span>
2028 [ 2, 3, 5, 7, 11 ]
2029 </pre></div>
2030
2031 <p><a id="X7DF510F7848CBBFD" name="X7DF510F7848CBBFD"></a></p>
2032
2033 <h4>21.20 <span class="Heading">Operations for Lists</span></h4>
2034
2035 <p>Several of the following functions expect the first argument to be either a list or a collection (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>), with possibly slightly different meaning for lists and non-list collections.</p>
2036
2037 <p><a id="X840C55A77D1BB2E1" name="X840C55A77D1BB2E1"></a></p>
2038
2039 <h5>21.20-1 Concatenation</h5>
2040
2041 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Concatenation</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2042 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Concatenation</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2043 <p>In the first form <code class="func">Concatenation</code> returns the concatenation of the lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc. The <em>concatenation</em> is the list that begins with the elements of <var class="Arg">list1</var>, followed by the elements of <var class="Arg">list2</var>, and so on. Each list may also contain holes, in which case the concatenation also contains holes at the corresponding positions.</p>
2044
2045 <p>In the second form <var class="Arg">list</var> must be a dense list of lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc., and <code class="func">Concatenation</code> returns the concatenation of those lists.</p>
2046
2047 <p>The result is a new mutable list, that is not identical to any other list. The elements of that list however are identical to the corresponding elements of <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc. (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>).</p>
2048
2049 <p>Note that <code class="func">Concatenation</code> creates a new list and leaves its arguments unchanged, while <code class="func">Append</code> (<a href="chap21.html#X79E31DB27C82D6E1"><span class="RefLink">21.4-5</span></a>) changes its first argument. For computing the union of proper sets, <code class="func">Union</code> (<a href="chap30.html#X799F0E2F7A502DBA"><span class="RefLink">30.5-3</span></a>) can be used, see also <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>.</p>
2050
2051
2052 <div class="example"><pre>
2053 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Concatenation( [ 1, 2, 3 ], [ 4, 5 ] );</span>
2054 [ 1, 2, 3, 4, 5 ]
2055 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Concatenation( [2,3,,5,,7], [11,,13,,,,17,,19] );</span>
2056 [ 2, 3,, 5,, 7, 11,, 13,,,, 17,, 19 ]
2057 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Concatenation( [ [1,2,3], [2,3,4], [3,4,5] ] );</span>
2058 [ 1, 2, 3, 2, 3, 4, 3, 4, 5 ]
2059 </pre></div>
2060
2061 <p><a id="X7CB0A6AF87C7FAF7" name="X7CB0A6AF87C7FAF7"></a></p>
2062
2063 <h5>21.20-2 Compacted</h5>
2064
2065 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Compacted</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2066 <p>returns a new mutable list that contains the elements of <var class="Arg">list</var> in the same order but omitting the holes.</p>
2067
2068
2069 <div class="example"><pre>
2070 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=[,1,,,3,,,4,[5,,,6],7];; Compacted( l );</span>
2071 [ 1, 3, 4, [ 5,,, 6 ], 7 ]
2072 </pre></div>
2073
2074 <p><a id="X7ECE9056792F28BA" name="X7ECE9056792F28BA"></a></p>
2075
2076 <h5>21.20-3 Collected</h5>
2077
2078 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Collected</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2079 <p>returns a new list <var class="Arg">new</var> that contains for each element <var class="Arg">elm</var> of the list <var class="Arg">list</var> a list of length two, the first element of this is <var class="Arg">elm</var> itself and the second element is the number of times <var class="Arg">elm</var> appears in <var class="Arg">list</var>. The order of those pairs in <var class="Arg">new</var> corresponds to the ordering of the elements elm, so that the result is sorted.</p>
2080
2081 <p>For all pairs of elements in <var class="Arg">list</var> the comparison via <code class="code">&lt;</code> must be defined.</p>
2082
2083
2084 <div class="example"><pre>
2085 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Factors( Factorial( 10 ) );</span>
2086 [ 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 5, 5, 7 ]
2087 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Collected( last );</span>
2088 [ [ 2, 8 ], [ 3, 4 ], [ 5, 2 ], [ 7, 1 ] ]
2089 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Collected( last );</span>
2090 [ [ [ 2, 8 ], 1 ], [ [ 3, 4 ], 1 ], [ [ 5, 2 ], 1 ], [ [ 7, 1 ], 1 ] ]
2091 </pre></div>
2092
2093 <p><a id="X8727F2928467C2F9" name="X8727F2928467C2F9"></a></p>
2094
2095 <h5>21.20-4 DuplicateFreeList</h5>
2096
2097 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DuplicateFreeList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2098 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Unique</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2099 <p>returns a new mutable list whose entries are the elements of the list <var class="Arg">list</var> with duplicates removed. <code class="func">DuplicateFreeList</code> only uses the <code class="code">=</code> comparison and will not sort the result. Therefore <code class="func">DuplicateFreeList</code> can be used even if the elements of <var class="Arg">list</var> do not lie in the same family. Otherwise, if <var class="Arg">list</var> contains objects that can be compared with <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>) then it is much more efficient to use <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) instead of <code class="func">DuplicateFreeList</code>.</p>
2100
2101 <p><code class="func">Unique</code> is a synonym for <code class="func">DuplicateFreeList</code>.</p>
2102
2103
2104 <div class="example"><pre>
2105 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=[1,Z(3),1,"abc",Group((1,2,3),(1,2)),Z(3),Group((1,2),(2,3))];;</span>
2106 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DuplicateFreeList( l );</span>
2107 [ 1, Z(3), "abc", Group([ (1,2,3), (1,2) ]) ]
2108 </pre></div>
2109
2110 <p><a id="X7F5D4DD87E4378AC" name="X7F5D4DD87E4378AC"></a></p>
2111
2112 <h5>21.20-5 AsDuplicateFreeList</h5>
2113
2114 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AsDuplicateFreeList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
2115 <p>returns the same result as <code class="func">DuplicateFreeList</code> (<a href="chap21.html#X8727F2928467C2F9"><span class="RefLink">21.20-4</span></a>), except that the result is immutable.</p>
2116
2117 <p><a id="X7FA272D984EF82ED" name="X7FA272D984EF82ED"></a></p>
2118
2119 <h5>21.20-6 Flat</h5>
2120
2121 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Flat</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2122 <p>returns the list of all elements that are contained in the list <var class="Arg">list</var> or its sublists. That is, <code class="func">Flat</code> first makes a new empty list <var class="Arg">new</var>. Then it loops over the elements <var class="Arg">elm</var> of <var class="Arg">list</var>. If <var class="Arg">elm</var> is not a list it is added to <var class="Arg">new</var>, otherwise <code class="func">Flat</code> appends <code class="code">Flat( <var class="Arg">elm</var> )</code> to <var class="Arg">new</var>.</p>
2123
2124
2125 <div class="example"><pre>
2126 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Flat( [ 1, [ 2, 3 ], [ [ 1, 2 ], 3 ] ] );</span>
2127 [ 1, 2, 3, 1, 2, 3 ]
2128 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Flat( [ ] );</span>
2129 [ ]
2130 </pre></div>
2131
2132 <p>To reconstruct a matrix from the list obtained by applying <code class="func">Flat</code> to the matrix, the sublist operator can be used, as follows.</p>
2133
2134
2135 <div class="example"><pre>
2136 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=[9..14];;w:=2;; # w is the length of each row</span>
2137 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sub:=[1..w];;List([1..Length(l)/w],i-&gt;l{(i-1)*w+sub});</span>
2138 [ [ 9, 10 ], [ 11, 12 ], [ 13, 14 ] ]
2139 </pre></div>
2140
2141 <p><a id="X7C4FDB007C3F54A1" name="X7C4FDB007C3F54A1"></a></p>
2142
2143 <h5>21.20-7 Reversed</h5>
2144
2145 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Reversed</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2146 <p>returns a new mutable list, containing the elements of the dense list <var class="Arg">list</var> in reversed order.</p>
2147
2148 <p>The argument list is unchanged. The result list is a new list, that is not identical to any other list. The elements of that list however are identical to the corresponding elements of the argument list (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>).</p>
2149
2150 <p><code class="func">Reversed</code> implements a special case of list assignment, which can also be formulated in terms of the <code class="code">{}</code> operator (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>).</p>
2151
2152 <p>Developers who wish to adapt this for custom list types need to install suitable methods for the operation <code class="code">ReversedOp</code>.</p>
2153
2154
2155 <div class="example"><pre>
2156 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Reversed( [ 1, 4, 9, 5, 6, 7 ] );</span>
2157 [ 7, 6, 5, 9, 4, 1 ]
2158 </pre></div>
2159
2160 <p><a id="X8057372F83374193" name="X8057372F83374193"></a></p>
2161
2162 <h5>21.20-8 Shuffle</h5>
2163
2164 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Shuffle</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2165 <p>The argument <var class="Arg">list</var> must be a dense mutable list. This operation permutes the entries of <var class="Arg">list</var> randomly (in place), and returns <var class="Arg">list</var>.</p>
2166
2167
2168 <div class="example"><pre>
2169 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Reset(GlobalMersenneTwister, 12345);; # make manual tester happy</span>
2170 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [1..20];</span>
2171 [ 1 .. 20 ]
2172 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m := Shuffle(ShallowCopy(l));</span>
2173 [ 8, 13, 1, 3, 20, 15, 4, 7, 5, 18, 6, 12, 16, 11, 2, 10, 19, 17, 9,
2174 14 ]
2175 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l;</span>
2176 [ 1 .. 20 ]
2177 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Shuffle(l);;</span>
2178 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l;</span>
2179 [ 19, 5, 7, 20, 16, 1, 10, 15, 12, 11, 13, 2, 14, 3, 4, 17, 6, 8, 9,
2180 18 ]
2181 </pre></div>
2182
2183 <p><a id="X8075FBDE7B81B4C8" name="X8075FBDE7B81B4C8"></a></p>
2184
2185 <h5>21.20-9 Apply</h5>
2186
2187 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Apply</code>( <var class="Arg">list</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2188 <p><code class="func">Apply</code> applies the function <var class="Arg">func</var> to every element of the dense and mutable list <var class="Arg">list</var>, and replaces each element entry by the corresponding return value.</p>
2189
2190 <p><code class="func">Apply</code> changes its argument. The nondestructive counterpart of <code class="func">Apply</code> is <code class="func">List</code> (<a href="chap30.html#X7F12F40E87F3C3A7"><span class="RefLink">30.3-5</span></a>).</p>
2191
2192
2193 <div class="example"><pre>
2194 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [ 1, 2, 3 ];; Apply( l, i -&gt; i^2 ); l;</span>
2195 [ 1, 4, 9 ]
2196 </pre></div>
2197
2198 <p><a id="X7EF6E2BC81DBF6FB" name="X7EF6E2BC81DBF6FB"></a></p>
2199
2200 <h5>21.20-10 Perform</h5>
2201
2202 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Perform</code>( <var class="Arg">list</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2203 <p><code class="func">Perform</code> applies the function <var class="Arg">func</var> to every element of the list <var class="Arg">list</var>, discarding any return values. It does not return a value.</p>
2204
2205
2206 <div class="example"><pre>
2207 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [1, 2, 3];; Perform(l,</span>
2208 <span class="GAPprompt">&gt;</span> <span class="GAPinput">function(x) if IsPrimeInt(x) then Print(x,"\n"); fi; end);</span>
2209 2
2210 3
2211 </pre></div>
2212
2213 <p><a id="X8763882A7D65F979" name="X8763882A7D65F979"></a></p>
2214
2215 <h5>21.20-11 PermListList</h5>
2216
2217 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PermListList</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2218 <p>returns a permutation <span class="SimpleMath">p</span> of <code class="code">[ 1 .. Length( <var class="Arg">list1</var> ) ]</code> such that <var class="Arg">list1</var><span class="SimpleMath">[i</span><code class="code">^</code><span class="SimpleMath">p] =</span> <var class="Arg">list2</var><span class="SimpleMath">[i]</span>. It returns <code class="keyw">fail</code> if there is no such permutation.</p>
2219
2220
2221 <div class="example"><pre>
2222 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list1 := [ 5, 4, 6, 1, 7, 5 ];;</span>
2223 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">list2 := [ 4, 1, 7, 5, 5, 6 ];;</span>
2224 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">perm := PermListList(list1, list2);</span>
2225 (1,2,4)(3,5,6)
2226 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Permuted( list2, perm );</span>
2227 [ 5, 4, 6, 1, 7, 5 ]
2228 </pre></div>
2229
2230 <p><a id="X82CE0DE8828E4303" name="X82CE0DE8828E4303"></a></p>
2231
2232 <h5>21.20-12 <span class="Heading">Maximum</span></h5>
2233
2234 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Maximum</code>( <var class="Arg">obj1</var>, <var class="Arg">obj2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2235 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Maximum</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2236 <p>In the first form <code class="func">Maximum</code> returns the <em>maximum</em> of its arguments, i.e., one argument <var class="Arg">obj</var> for which <span class="SimpleMath"><var class="Arg">obj</var> ≥ <var class="Arg">obj1</var></span>, <span class="SimpleMath"><var class="Arg">obj</var> ≥ <var class="Arg">obj2</var></span> etc.</p>
2237
2238 <p>In the second form <code class="func">Maximum</code> takes a homogeneous list <var class="Arg">list</var> and returns the maximum of the elements in this list.</p>
2239
2240
2241 <div class="example"><pre>
2242 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Maximum( -123, 700, 123, 0, -1000 );</span>
2243 700
2244 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Maximum( [ -123, 700, 123, 0, -1000 ] );</span>
2245 700
2246 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># lists are compared elementwise:</span>
2247 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Maximum( [1,2], [0,15], [1,5], [2,-11] );</span>
2248 [ 2, -11 ]
2249 </pre></div>
2250
2251 <p>To get the index of the maximum element use <code class="func">PositionMaximum</code> (<a href="chap21.html#X7FD9C1D37F300206"><span class="RefLink">21.16-8</span></a>)</p>
2252
2253 <p><a id="X82F133EC7F89665F" name="X82F133EC7F89665F"></a></p>
2254
2255 <h5>21.20-13 <span class="Heading">Minimum</span></h5>
2256
2257 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Minimum</code>( <var class="Arg">obj1</var>, <var class="Arg">obj2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2258 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Minimum</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2259 <p>In the first form <code class="func">Minimum</code> returns the <em>minimum</em> of its arguments, i.e., one argument <var class="Arg">obj</var> for which <span class="SimpleMath"><var class="Arg">obj</var> ≤ <var class="Arg">obj1</var></span>, <span class="SimpleMath"><var class="Arg">obj</var> ≤ <var class="Arg">obj2</var></span> etc.</p>
2260
2261 <p>In the second form <code class="func">Minimum</code> takes a homogeneous list <var class="Arg">list</var> and returns the minimum of the elements in this list.</p>
2262
2263 <p>Note that for both <code class="func">Maximum</code> (<a href="chap21.html#X82CE0DE8828E4303"><span class="RefLink">21.20-12</span></a>) and <code class="func">Minimum</code> the comparison of the objects <var class="Arg">obj1</var>, <var class="Arg">obj2</var> etc. must be defined; for that, usually they must lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>).</p>
2264
2265
2266 <div class="example"><pre>
2267 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Minimum( -123, 700, 123, 0, -1000 );</span>
2268 -1000
2269 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Minimum( [ -123, 700, 123, 0, -1000 ] );</span>
2270 -1000
2271 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Minimum( [ 1, 2 ], [ 0, 15 ], [ 1, 5 ], [ 2, -11 ] );</span>
2272 [ 0, 15 ]
2273 </pre></div>
2274
2275 <p>To get the index of the minimum element use <code class="func">PositionMinimum</code> (<a href="chap21.html#X7FD9C1D37F300206"><span class="RefLink">21.16-8</span></a>)</p>
2276
2277 <p><a id="X842851EB7E0969F7" name="X842851EB7E0969F7"></a></p>
2278
2279 <h5>21.20-14 <span class="Heading">MaximumList and MinimumList</span></h5>
2280
2281 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MaximumList</code>( <var class="Arg">list</var>[, <var class="Arg">seed</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2282 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MinimumList</code>( <var class="Arg">list</var>[, <var class="Arg">seed</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2283 <p>return the maximum resp. the minimum of the elements in the list <var class="Arg">list</var>. They are the operations called by <code class="func">Maximum</code> (<a href="chap21.html#X82CE0DE8828E4303"><span class="RefLink">21.20-12</span></a>) resp. <code class="func">Minimum</code> (<a href="chap21.html#X82F133EC7F89665F"><span class="RefLink">21.20-13</span></a>). Methods can be installed for special kinds of lists. For example, there are special methods to compute the maximum resp. the minimum of a range (see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a>).</p>
2284
2285 <p>If a second argument <var class="Arg">seed</var> is supplied, then the result is the maximum resp. minimum of the union of <var class="Arg">list</var> and <var class="Arg">seed</var>. In this manner, the operations may be applied to empty lists.</p>
2286
2287 <p><a id="X7E1593B979BDF2CD" name="X7E1593B979BDF2CD"></a></p>
2288
2289 <h5>21.20-15 <span class="Heading">Cartesian</span></h5>
2290
2291 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Cartesian</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2292 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Cartesian</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2293 <p>In the first form <code class="func">Cartesian</code> returns the cartesian product of the lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc.</p>
2294
2295 <p>In the second form <var class="Arg">list</var> must be a list of lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc., and <code class="func">Cartesian</code> returns the cartesian product of those lists.</p>
2296
2297 <p>The <em>cartesian product</em> is a list <var class="Arg">cart</var> of lists <var class="Arg">tup</var>, such that the first element of <var class="Arg">tup</var> is an element of <var class="Arg">list1</var>, the second element of <var class="Arg">tup</var> is an element of <var class="Arg">list2</var>, and so on. The total number of elements in <var class="Arg">cart</var> is the product of the lengths of the argument lists. In particular <var class="Arg">cart</var> is empty if and only if at least one of the argument lists is empty. Also <var class="Arg">cart</var> contains duplicates if and only if no argument list is empty and at least one contains duplicates.</p>
2298
2299 <p>The last index runs fastest. That means that the first element <var class="Arg">tup1</var> of <var class="Arg">cart</var> contains the first element from <var class="Arg">list1</var>, from <var class="Arg">list2</var> and so on. The second element <var class="Arg">tup2</var> of <var class="Arg">cart</var> contains the first element from <var class="Arg">list1</var>, the first from <var class="Arg">list2</var>, and so on, but the last element of <var class="Arg">tup2</var> is the second element of the last argument list. This implies that <var class="Arg">cart</var> is a proper set if and only if all argument lists are proper sets (see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>).</p>
2300
2301 <p>The function <code class="func">Tuples</code> (<a href="chap16.html#X86A3CA0F7CC8C320"><span class="RefLink">16.2-8</span></a>) computes the <var class="Arg">k</var>-fold cartesian product of a list.</p>
2302
2303
2304 <div class="example"><pre>
2305 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Cartesian( [1,2], [3,4], [5,6] );</span>
2306 [ [ 1, 3, 5 ], [ 1, 3, 6 ], [ 1, 4, 5 ], [ 1, 4, 6 ], [ 2, 3, 5 ],
2307 [ 2, 3, 6 ], [ 2, 4, 5 ], [ 2, 4, 6 ] ]
2308 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Cartesian( [1,2,2], [1,1,2] );</span>
2309 [ [ 1, 1 ], [ 1, 1 ], [ 1, 2 ], [ 2, 1 ], [ 2, 1 ], [ 2, 2 ],
2310 [ 2, 1 ], [ 2, 1 ], [ 2, 2 ] ]
2311 </pre></div>
2312
2313 <p><a id="X7E76F5A782184823" name="X7E76F5A782184823"></a></p>
2314
2315 <h5>21.20-16 <span class="Heading">IteratorOfCartesianProduct</span></h5>
2316
2317 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IteratorOfCartesianProduct</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2318 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IteratorOfCartesianProduct</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2319 <p>In the first form <code class="func">IteratorOfCartesianProduct</code> returns an iterator (see <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a>) of all elements of the cartesian product (see <code class="func">Cartesian</code> (<a href="chap21.html#X7E1593B979BDF2CD"><span class="RefLink">21.20-15</span></a>)) of the lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc.</p>
2320
2321 <p>In the second form <var class="Arg">list</var> must be a list of lists <var class="Arg">list1</var>, <var class="Arg">list2</var>, etc., and <code class="func">IteratorOfCartesianProduct</code> returns an iterator of the cartesian product of those lists.</p>
2322
2323 <p>Resulting tuples will be returned in the lexicographic order. Usage of iterators of cartesian products is recommended in the case when the resulting cartesian product is big enough, so its generating and storage will require essential amount of runtime and memory. For smaller cartesian products it is faster to generate the full set of tuples using <code class="func">Cartesian</code> (<a href="chap21.html#X7E1593B979BDF2CD"><span class="RefLink">21.20-15</span></a>) and then loop over its elements (with some minor overhead of needing more memory).</p>
2324
2325 <p><a id="X7B5A19098406347A" name="X7B5A19098406347A"></a></p>
2326
2327 <h5>21.20-17 Permuted</h5>
2328
2329 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Permuted</code>( <var class="Arg">list</var>, <var class="Arg">perm</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2330 <p>returns a new list <var class="Arg">new</var> that contains the elements of the list <var class="Arg">list</var> permuted according to the permutation <var class="Arg">perm</var>. That is <code class="code"><var class="Arg">new</var>[<var class="Arg">i</var>^<var class="Arg">perm</var>] = <var class="Arg">list</var>[<var class="Arg">i</var>]</code> whenever <code class="code"><var class="Arg">list</var>[<var class="Arg">i</var>]</code> is bound.</p>
2331
2332 <p><code class="func">Sortex</code> (<a href="chap21.html#X87287FCA81E2B06A"><span class="RefLink">21.18-3</span></a>) allows you to compute a permutation that must be applied to a list in order to get the sorted list.</p>
2333
2334
2335 <div class="example"><pre>
2336 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Permuted( [ 5, 4, 6, 1, 7, 5 ], (1,3,5,6,4) );</span>
2337 [ 1, 4, 5, 5, 6, 7 ]
2338 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Permuted( [ 5, 4, 6,, 7, 5 ], (1,3,5,6,4) );</span>
2339 [ , 4, 5, 5, 6, 7 ]
2340 </pre></div>
2341
2342 <p><a id="X86CB7DCE8510F977" name="X86CB7DCE8510F977"></a></p>
2343
2344 <h5>21.20-18 List</h5>
2345
2346 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; List</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2347 <p>This function returns a new mutable list <code class="code">new</code> of the same length as the list <var class="Arg">list</var> (which may have holes). The entry <code class="code">new[i]</code> is unbound if <code class="code"><var class="Arg">list</var>[i]</code> is unbound. Otherwise <code class="code">new[i] = <var class="Arg">func</var>(<var class="Arg">list</var>[i])</code>. If the argument <var class="Arg">func</var> is omitted, its default is <code class="func">IdFunc</code> (<a href="chap5.html#X810325697BDEF899"><span class="RefLink">5.4-6</span></a>), so this function does the same as <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) (see also <a href="chap21.html#X7ED7C0738495556F"><span class="RefLink">21.7</span></a>).</p>
2348
2349 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">ListOp</code>.</p>
2350
2351
2352 <div class="example"><pre>
2353 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( [1,2,3], i -&gt; i^2 );</span>
2354 [ 1, 4, 9 ]
2355 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( [1..10], IsPrime );</span>
2356 [ false, true, true, false, true, false, true, false, false, false ]
2357 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([,1,,3,4], x-&gt; x &gt; 2);</span>
2358 [ , false,, true, true ]
2359 </pre></div>
2360
2361 <p>(See also <code class="func">List</code> (<a href="chap30.html#X7F12F40E87F3C3A7"><span class="RefLink">30.3-5</span></a>).)</p>
2362
2363 <p><a id="X7C86D7F7795125F0" name="X7C86D7F7795125F0"></a></p>
2364
2365 <h5>21.20-19 Filtered</h5>
2366
2367 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Filtered</code>( <var class="Arg">listorcoll</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2368 <p>returns a new list that contains those elements of the list or collection <var class="Arg">listorcoll</var> (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>), respectively, for which the unary function <var class="Arg">func</var> returns <code class="keyw">true</code>.</p>
2369
2370 <p>If the first argument is a list, the order of the elements in the result is the same as the order of the corresponding elements of this list. If an element for which <var class="Arg">func</var> returns <code class="keyw">true</code> appears several times in the list it will also appear the same number of times in the result. The argument list may contain holes, they are ignored by <code class="func">Filtered</code>.</p>
2371
2372 <p>For each element of <var class="Arg">listorcoll</var>, <var class="Arg">func</var> must return either <code class="keyw">true</code> or <code class="keyw">false</code>, otherwise an error is signalled.</p>
2373
2374 <p>The result is a new list that is not identical to any other list. The elements of that list however are identical to the corresponding elements of the argument list (see <a href="chap21.html#X7DD65BEA7EDB0CD7"><span class="RefLink">21.6</span></a>).</p>
2375
2376 <p>List assignment using the operator <code class="func">\{\}</code> (<a href="chap21.html#X78791B8B838A8BA0"><span class="RefLink">21.3-1</span></a>) (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>) can be used to extract elements of a list according to indices given in another list.</p>
2377
2378 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">FilteredOp</code>.</p>
2379
2380
2381 <div class="example"><pre>
2382 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered( [1..20], IsPrime );</span>
2383 [ 2, 3, 5, 7, 11, 13, 17, 19 ]
2384 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered( [ 1, 3, 4, -4, 4, 7, 10, 6 ], IsPrimePowerInt );</span>
2385 [ 3, 4, 4, 7 ]
2386 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered( [ 1, 3, 4, -4, 4, 7, 10, 6 ],</span>
2387 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; IsPrimePowerInt(n) and n mod 2 &lt;&gt; 0 );</span>
2388 [ 3, 7 ]
2389 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered( Group( (1,2), (1,2,3) ), x -&gt; Order( x ) = 2 );</span>
2390 [ (2,3), (1,2), (1,3) ]
2391 </pre></div>
2392
2393 <p><a id="X8179B13D80E935FC" name="X8179B13D80E935FC"></a></p>
2394
2395 <h5>21.20-20 Number</h5>
2396
2397 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Number</code>( <var class="Arg">listorcoll</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2398 <p>Called with a list <var class="Arg">listorcoll</var>, <code class="func">Number</code> returns the number of bound entries in this list. For dense lists <code class="func">Number</code>, <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), and <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) return the same value; for lists with holes <code class="func">Number</code> returns the number of bound entries, <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>) returns the largest index of a bound entry, and <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) signals an error.</p>
2399
2400 <p>Called with two arguments, a list or collection <var class="Arg">listorcoll</var> and a unary function <var class="Arg">func</var>, <code class="func">Number</code> returns the number of elements of <var class="Arg">listorcoll</var> for which <var class="Arg">func</var> returns <code class="keyw">true</code>. If an element for which <var class="Arg">func</var> returns <code class="keyw">true</code> appears several times in <var class="Arg">listorcoll</var> it will also be counted the same number of times.</p>
2401
2402 <p>For each element of <var class="Arg">listorcoll</var>, <var class="Arg">func</var> must return either <code class="keyw">true</code> or <code class="keyw">false</code>, otherwise an error is signalled.</p>
2403
2404 <p><code class="func">Filtered</code> (<a href="chap21.html#X7C86D7F7795125F0"><span class="RefLink">21.20-19</span></a>) allows you to extract the elements of a list that have a certain property.</p>
2405
2406 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">NumberOp</code>.</p>
2407
2408
2409 <div class="example"><pre>
2410 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( [ 2, 3, 5, 7 ] );</span>
2411 4
2412 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( [, 2, 3,, 5,, 7,,,, 11 ] );</span>
2413 5
2414 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( [1..20], IsPrime );</span>
2415 8
2416 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( [ 1, 3, 4, -4, 4, 7, 10, 6 ], IsPrimePowerInt );</span>
2417 4
2418 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( [ 1, 3, 4, -4, 4, 7, 10, 6 ],</span>
2419 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; IsPrimePowerInt(n) and n mod 2 &lt;&gt; 0 );</span>
2420 2
2421 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Number( Group( (1,2), (1,2,3) ), x -&gt; Order( x ) = 2 );</span>
2422 3
2423 </pre></div>
2424
2425 <p><a id="X82801DFA84E11272" name="X82801DFA84E11272"></a></p>
2426
2427 <h5>21.20-21 First</h5>
2428
2429 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; First</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2430 <p><code class="func">First</code> returns the first element of the list <var class="Arg">list</var> for which the unary function <var class="Arg">func</var> returns <code class="keyw">true</code>; if <var class="Arg">func</var> is not given, the first element is returned. <var class="Arg">list</var> may contain holes. <var class="Arg">func</var> must return either <code class="keyw">true</code> or <code class="keyw">false</code> for each element of <var class="Arg">list</var>, otherwise an error is signalled. If <var class="Arg">func</var> returns <code class="keyw">false</code> for all elements of <var class="Arg">list</var> then <code class="func">First</code> returns <code class="keyw">fail</code>.</p>
2431
2432 <p><code class="func">PositionProperty</code> (<a href="chap21.html#X7E6C763A82C6153B"><span class="RefLink">21.16-9</span></a>) allows you to find the position of the first element in a list that satisfies a certain property.</p>
2433
2434 <p>Before <strong class="pkg">GAP</strong> 4.12, developers who wished to adapt this for custom list types needed to install suitable methods for the operation <code class="code">FirstOp</code>. This is still possible for backwards compatibility, but <code class="code">FirstOp</code> now is just a synonym for <code class="func">First</code>.</p>
2435
2436
2437 <div class="example"><pre>
2438 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">First( [10^7..10^8], IsPrime );</span>
2439 10000019
2440 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">First( [10^5..10^6],</span>
2441 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; not IsPrime(n) and IsPrimePowerInt(n) );</span>
2442 100489
2443 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">First( [ 1 .. 20 ], x -&gt; x &lt; 0 );</span>
2444 fail
2445 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">First( [ fail ], x -&gt; x = fail );</span>
2446 fail
2447 </pre></div>
2448
2449 <p><a id="X7E5B62E780421CE9" name="X7E5B62E780421CE9"></a></p>
2450
2451 <h5>21.20-22 Last</h5>
2452
2453 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Last</code>( <var class="Arg">list</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2454 <p><code class="func">Last</code> returns the last element of the list <var class="Arg">list</var> for which the unary function <var class="Arg">func</var> returns <code class="keyw">true</code>; if <var class="Arg">func</var> is not given, the last element is returned. <var class="Arg">list</var> may contain holes. <var class="Arg">func</var> must return either <code class="keyw">true</code> or <code class="keyw">false</code> for each element of <var class="Arg">list</var>, otherwise an error is signalled. If <var class="Arg">func</var> returns <code class="keyw">false</code> for all elements of <var class="Arg">list</var> then <code class="func">Last</code> returns <code class="keyw">fail</code>.</p>
2455
2456 <p>Developers who wish to adapt this for custom list types need to install suitable methods for the operation <code class="code">LastOp</code>.</p>
2457
2458
2459 <div class="example"><pre>
2460 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Last( [10^7..10^8], IsPrime );</span>
2461 99999989
2462 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Last( [10^5..10^6],</span>
2463 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; not IsPrime(n) and IsPrimePowerInt(n) );</span>
2464 994009
2465 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Last( [ 1 .. 20 ], x -&gt; x &lt; 0 );</span>
2466 fail
2467 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Last( [ fail ], x -&gt; x = fail );</span>
2468 fail
2469 </pre></div>
2470
2471 <p><a id="X7F06961278166671" name="X7F06961278166671"></a></p>
2472
2473 <h5>21.20-23 ForAll</h5>
2474
2475 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ForAll</code>( <var class="Arg">listorcoll</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2476 <p>tests whether the unary function <var class="Arg">func</var> returns <code class="keyw">true</code> for all elements in the list or collection <var class="Arg">listorcoll</var>.</p>
2477
2478 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">ForAllOp</code>.</p>
2479
2480
2481 <div class="example"><pre>
2482 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( [1..20], IsPrime );</span>
2483 false
2484 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( [2,3,4,5,8,9], IsPrimePowerInt );</span>
2485 true
2486 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( [2..14], n -&gt; IsPrimePowerInt(n) or n mod 2 = 0 );</span>
2487 true
2488 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAll( Group( (1,2), (1,2,3) ), i -&gt; SignPerm(i) = 1 );</span>
2489 false
2490 </pre></div>
2491
2492 <p><a id="X7AF82E747A8BDA75" name="X7AF82E747A8BDA75"></a></p>
2493
2494 <h5>21.20-24 ForAny</h5>
2495
2496 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ForAny</code>( <var class="Arg">listorcoll</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2497 <p>tests whether the unary function <var class="Arg">func</var> returns <code class="keyw">true</code> for at least one element in the list or collection <var class="Arg">listorcoll</var>.</p>
2498
2499 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">ForAnyOp</code>.</p>
2500
2501
2502 <div class="example"><pre>
2503 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAny( [1..20], IsPrime );</span>
2504 true
2505 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAny( [2,3,4,5,8,9], IsPrimePowerInt );</span>
2506 true
2507 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAny( [2..14],</span>
2508 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> n -&gt; IsPrimePowerInt(n) and n mod 5 = 0 and not IsPrime(n) );</span>
2509 false
2510 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAny( Integers, i -&gt; i &gt; 0</span>
2511 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> and ForAll( [0,2..4], j -&gt; IsPrime(i+j) ) );</span>
2512 true
2513 </pre></div>
2514
2515 <p><a id="X7E5C72F27B657948" name="X7E5C72F27B657948"></a></p>
2516
2517 <h5>21.20-25 Product</h5>
2518
2519 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Product</code>( <var class="Arg">listorcoll</var>[, <var class="Arg">func</var>][, <var class="Arg">init</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2520 <p>Called with one argument, a dense list or collection <var class="Arg">listorcoll</var>, <code class="func">Product</code> returns the product of the elements of <var class="Arg">listorcoll</var> (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>).</p>
2521
2522 <p>Called with a dense list or collection <var class="Arg">listorcoll</var> and a function <var class="Arg">func</var>, which must be a function taking one argument, <code class="func">Product</code> applies the function <var class="Arg">func</var> to the elements of <var class="Arg">listorcoll</var>, and returns the product of the results. In either case <code class="func">Product</code> returns <code class="code">1</code> if the first argument is empty.</p>
2523
2524 <p>The general rules for arithmetic operations apply (see <a href="chap21.html#X8676EFE67972FD06"><span class="RefLink">21.15</span></a>), so the result is immutable if and only if all summands are immutable.</p>
2525
2526 <p>If <var class="Arg">listorcoll</var> contains exactly one element then this element (or its image under <var class="Arg">func</var> if applicable) itself is returned, not a shallow copy of this element.</p>
2527
2528 <p>If an additional initial value <var class="Arg">init</var> is given, <code class="func">Product</code> returns the product of <var class="Arg">init</var> and the elements of the first argument resp. of their images under the function <var class="Arg">func</var>. This is useful for example if the first argument is empty and a different identity than <code class="code">1</code> is desired, in which case <var class="Arg">init</var> is returned.</p>
2529
2530 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">ProductOp</code>.</p>
2531
2532
2533 <div class="example"><pre>
2534 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product( [ 2, 3, 5, 7, 11, 13, 17, 19 ] );</span>
2535 9699690
2536 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product( [1..10], x-&gt;x^2 );</span>
2537 13168189440000
2538 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product( [ (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) ] );</span>
2539 (1,4)(2,3)
2540 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Product( GF(8) );</span>
2541 0*Z(2)
2542 </pre></div>
2543
2544 <p><a id="X7A04B71C84CFCC2D" name="X7A04B71C84CFCC2D"></a></p>
2545
2546 <h5>21.20-26 Sum</h5>
2547
2548 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Sum</code>( <var class="Arg">listorcoll</var>[, <var class="Arg">func</var>][, <var class="Arg">init</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2549 <p>Called with one argument, a dense list or collection <var class="Arg">listorcoll</var>, <code class="func">Sum</code> returns the sum of the elements of <var class="Arg">listorcoll</var> (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>).</p>
2550
2551 <p>Called with a dense list or collection <var class="Arg">listorcoll</var> and a function <var class="Arg">func</var>, which must be a function taking one argument, <code class="func">Sum</code> applies the function <var class="Arg">func</var> to the elements of <var class="Arg">listorcoll</var>, and returns the sum of the results. In either case <code class="func">Sum</code> returns <code class="code">0</code> if the first argument is empty.</p>
2552
2553 <p>The general rules for arithmetic operations apply (see <a href="chap21.html#X8676EFE67972FD06"><span class="RefLink">21.15</span></a>), so the result is immutable if and only if all summands are immutable.</p>
2554
2555 <p>If <var class="Arg">listorcoll</var> contains exactly one element then this element (or its image under <var class="Arg">func</var> if applicable) itself is returned, not a shallow copy of this element.</p>
2556
2557 <p>If an additional initial value <var class="Arg">init</var> is given, <code class="func">Sum</code> returns the sum of <var class="Arg">init</var> and the elements of the first argument resp. of their images under the function <var class="Arg">func</var>. This is useful for example if the first argument is empty and a different zero than <code class="code">0</code> is desired, in which case <var class="Arg">init</var> is returned.</p>
2558
2559 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">SumOp</code>.</p>
2560
2561
2562 <div class="example"><pre>
2563 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sum( [ 2, 3, 5, 7, 11, 13, 17, 19 ] );</span>
2564 77
2565 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sum( [1..10], x-&gt;x^2 );</span>
2566 385
2567 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sum( [ [1,2], [3,4], [5,6] ] );</span>
2568 [ 9, 12 ]
2569 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sum( GF(8) );</span>
2570 0*Z(2)
2571 </pre></div>
2572
2573 <p><a id="X834E4DF57F3A20F0" name="X834E4DF57F3A20F0"></a></p>
2574
2575 <h5>21.20-27 Iterated</h5>
2576
2577 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Iterated</code>( <var class="Arg">list</var>, <var class="Arg">f</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2578 <p>returns the result of the iterated application of the function <var class="Arg">f</var>, which must take two arguments, to the elements of the list <var class="Arg">list</var>. More precisely, if <var class="Arg">list</var> has length <span class="SimpleMath">n</span> then <code class="func">Iterated</code> returns the result of the following application, <span class="SimpleMath"><var class="Arg">f</var>( ... <var class="Arg">f</var>( <var class="Arg">f</var>( <var class="Arg">list</var>[1], <var class="Arg">list</var>[2] ), <var class="Arg">list</var>[3] ), ..., <var class="Arg">list</var>[n] )</span>.</p>
2579
2580
2581 <div class="example"><pre>
2582 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Iterated( [ 126, 66, 105 ], Gcd );</span>
2583 3
2584 </pre></div>
2585
2586 <p><a id="X7D150C2881881139" name="X7D150C2881881139"></a></p>
2587
2588 <h5>21.20-28 ListN</h5>
2589
2590 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ListN</code>( <var class="Arg">list1</var>, <var class="Arg">list2</var>, <var class="Arg">...</var>, <var class="Arg">listn</var>, <var class="Arg">f</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2591 <p>applies the <span class="SimpleMath">n</span>-argument function <var class="Arg">f</var> to the lists. That is, <code class="func">ListN</code> returns the list whose <span class="SimpleMath">i</span>-th entry is <span class="SimpleMath"><var class="Arg">f</var>(<var class="Arg">list1</var>[i], <var class="Arg">list2</var>[i], ..., <var class="Arg">listn</var>[i])</span>.</p>
2592
2593
2594 <div class="example"><pre>
2595 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ListN( [1,2], [3,4], \+ );</span>
2596 [ 4, 6 ]
2597 </pre></div>
2598
2599 <p><a id="X805CA0B68029B47A" name="X805CA0B68029B47A"></a></p>
2600
2601 <h4>21.21 <span class="Heading">Advanced List Manipulations</span></h4>
2602
2603 <p>The following functions are generalizations of <code class="func">List</code> (<a href="chap30.html#X7F12F40E87F3C3A7"><span class="RefLink">30.3-5</span></a>), <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>), <code class="func">Sum</code> (<a href="chap21.html#X7A04B71C84CFCC2D"><span class="RefLink">21.20-26</span></a>), <code class="func">Product</code> (<a href="chap21.html#X7E5C72F27B657948"><span class="RefLink">21.20-25</span></a>), <code class="func">ForAll</code> (<a href="chap21.html#X7F06961278166671"><span class="RefLink">21.20-23</span></a>), <code class="func">ForAny</code> (<a href="chap21.html#X7AF82E747A8BDA75"><span class="RefLink">21.20-24</span></a>), <code class="func">Filtered</code> (<a href="chap21.html#X7C86D7F7795125F0"><span class="RefLink">21.20-19</span></a>), <code class="func">Number</code> (<a href="chap21.html#X8179B13D80E935FC"><span class="RefLink">21.20-20</span></a>), and <code class="func">Perform</code> (<a href="chap21.html#X7EF6E2BC81DBF6FB"><span class="RefLink">21.20-10</span></a>).</p>
2604
2605 <p><a id="X799A66417CDC1D4F" name="X799A66417CDC1D4F"></a></p>
2606
2607 <h5>21.21-1 FoldLeft</h5>
2608
2609 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FoldLeft</code>( <var class="Arg">C</var>, <var class="Arg">func</var>[, <var class="Arg">init</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
2610 <p><code class="func">FoldLeft</code> applies the binary function <var class="Arg">func</var> left-associatively to the elements of the list or collection <var class="Arg">C</var>.</p>
2611
2612 <p>If no initial accumulator <var class="Arg">init</var> is given, the first element of <var class="Arg">C</var> is used as the initial accumulator and the remaining elements are combined with it from left to right. In that case <var class="Arg">C</var> must not be empty.</p>
2613
2614 <p>If <var class="Arg">init</var> is given, the accumulator is initialized with <var class="Arg">init</var>. Then an empty list or collection is allowed, and <var class="Arg">init</var> is returned unchanged in that case.</p>
2615
2616 <p>If <var class="Arg">C</var> is a list with holes, these holes are ignored, just as when iterating over the list with a <code class="code">for</code>-loop.</p>
2617
2618 <p>This operation can be used to express reductions similar to <code class="func">Sum</code> (<a href="chap21.html#X7A04B71C84CFCC2D"><span class="RefLink">21.20-26</span></a>) or <code class="func">Product</code> (<a href="chap21.html#X7E5C72F27B657948"><span class="RefLink">21.20-25</span></a>), but it is more general because the accumulator is updated by an arbitrary binary function.</p>
2619
2620
2621 <div class="example"><pre>
2622 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FoldLeft( [ 1 .. 10 ], \+ );</span>
2623 55
2624 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FoldLeft( [ 1 .. 4 ], \*, 2 );</span>
2625 48
2626 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FoldLeft( [ 1,, 3 ], { a, b } -&gt; a + b );</span>
2627 4
2628 </pre></div>
2629
2630 <p><a id="X83A2CF7A7F6C1DB9" name="X83A2CF7A7F6C1DB9"></a></p>
2631
2632 <h5>21.21-2 FoldLeftX</h5>
2633
2634 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FoldLeftX</code>( <var class="Arg">gens</var>, <var class="Arg">func</var>, <var class="Arg">init</var>[, <var class="Arg">abortValue</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2635 <p><code class="func">FoldLeftX</code> is a generalization of <code class="func">FoldLeft</code> (<a href="chap21.html#X799A66417CDC1D4F"><span class="RefLink">21.21-1</span></a>) which applies an accumulation function <var class="Arg">func</var> to a bunch of inputs which are derived from <var class="Arg">gens</var>.</p>
2636
2637 <p>The argument <var class="Arg">gens</var> must be a plain list whose entries describe a sequence of nested loops and filters. Specifically, let <code class="code">n</code> denote the length of <var class="Arg">gens</var>. Then each of the entries <var class="Arg">gens</var><code class="code">[1]</code>, <span class="SimpleMath">...</span> <var class="Arg">gens</var><code class="code">[n]</code> must be one of the following:</p>
2638
2639
2640 <dl>
2641 <dt><strong class="Mark">a list or collection</strong></dt>
2642 <dd><p>this introduces a new for-loop in the sequence of nested for-loops and if-statements;</p>
2643
2644 </dd>
2645 <dt><strong class="Mark">a function returning a list or collection</strong></dt>
2646 <dd><p>this introduces a new for-loop in the sequence of nested for-loops and if-statements, where the loop-range depends on the values of the outer loop-variables; or</p>
2647
2648 </dd>
2649 <dt><strong class="Mark">a function returning <code class="keyw">true</code> or <code class="keyw">false</code></strong></dt>
2650 <dd><p>this introduces a new if-statement in the sequence of nested for-loops and if-statements.</p>
2651
2652 </dd>
2653 </dl>
2654 <p>Each function occurring in <var class="Arg">gens</var> is called with the values selected by the preceding entries of <var class="Arg">gens</var>, in order. Thus the function in <var class="Arg">gens</var><code class="code">[k]</code> receives one argument for each of <var class="Arg">gens</var><code class="code">[1]</code>, <span class="SimpleMath">...</span>, <var class="Arg">gens</var><code class="code">[k-1]</code> that introduced a loop variable.</p>
2655
2656 <p>The argument <var class="Arg">func</var> must be a binary function, whose first argument is an accumulator variable, and the second argument is the tuple of values of the loop-variables.</p>
2657
2658 <p>The accumulator is initialized with <var class="Arg">init</var>. For every tuple that is produced by the loops and filters described by <var class="Arg">gens</var>, <var class="Arg">func</var> is called with the current accumulator and that tuple. Its return value becomes the new accumulator.</p>
2659
2660 <p>If the optional argument <var class="Arg">abortValue</var> is given, iteration stops as soon as the accumulator becomes identical to <var class="Arg">abortValue</var>. This is useful for short-circuiting computations such as <code class="func">ForAllX</code> (<a href="chap21.html#X86CF6FC87C5C9A08"><span class="RefLink">21.21-7</span></a>) and <code class="func">ForAnyX</code> (<a href="chap21.html#X858C7CEF7EC1260C"><span class="RefLink">21.21-8</span></a>).</p>
2661
2662
2663 <div class="example"><pre>
2664 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FoldLeftX( [ [ 1 .. 3 ], i -&gt; [ 1 .. i ],</span>
2665 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> { i, j } -&gt; i &lt;&gt; j ],</span>
2666 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> { acc, x } -&gt; acc + 1, 0 );</span>
2667 3
2668 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FoldLeftX( [ [ 1 .. 3 ], [ 1 .. 3 ], \&lt; ],</span>
2669 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> function( acc, x )</span>
2670 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Add( acc, ShallowCopy( x ) );</span>
2671 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return acc;</span>
2672 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end, [] );</span>
2673 [ [ 1, 2 ], [ 1, 3 ], [ 2, 3 ] ]
2674 </pre></div>
2675
2676 <p><a id="X8258477D7F72171B" name="X8258477D7F72171B"></a></p>
2677
2678 <h5>21.21-3 ListX</h5>
2679
2680 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ListX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2681 <p><code class="func">ListX</code> returns a new list constructed from the arguments. The general mechanism underlying this and the related <q>X</q>-functions is described in <code class="func">FoldLeftX</code> (<a href="chap21.html#X83A2CF7A7F6C1DB9"><span class="RefLink">21.21-2</span></a>).</p>
2682
2683 <p>Each of the arguments <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <span class="SimpleMath">...</span> <var class="Arg">argn</var> must be one of the following:</p>
2684
2685
2686 <dl>
2687 <dt><strong class="Mark">a list or collection</strong></dt>
2688 <dd><p>this introduces a new for-loop in the sequence of nested for-loops and if-statements;</p>
2689
2690 </dd>
2691 <dt><strong class="Mark">a function returning a list or collection</strong></dt>
2692 <dd><p>this introduces a new for-loop in the sequence of nested for-loops and if-statements, where the loop-range depends on the values of the outer loop-variables; or</p>
2693
2694 </dd>
2695 <dt><strong class="Mark">a function returning <code class="keyw">true</code> or <code class="keyw">false</code></strong></dt>
2696 <dd><p>this introduces a new if-statement in the sequence of nested for-loops and if-statements.</p>
2697
2698 </dd>
2699 </dl>
2700 <p>Each function occurring among the arguments is called with the values selected by the preceding arguments, in order. Thus the function in the <var class="Arg">k</var>-th argument position receives one argument for each preceding argument that introduced a loop variable.</p>
2701
2702 <p>The last argument <var class="Arg">func</var> must be a function, it is applied to the values of the loop-variables and the results are collected.</p>
2703
2704 <p>Thus <code class="code">ListX( <var class="Arg">list</var>, <var class="Arg">func</var> )</code> is the same as <code class="code">List( <var class="Arg">list</var>, <var class="Arg">func</var> )</code>, and <code class="code">ListX( <var class="Arg">list</var>, <var class="Arg">func</var>, x -&gt; x )</code> is the same as <code class="code">Filtered( <var class="Arg">list</var>, <var class="Arg">func</var> )</code>.</p>
2705
2706 <p>As a more elaborate example, assume <var class="Arg">arg1</var> is a list or collection, <var class="Arg">arg2</var> is a function returning <code class="keyw">true</code> or <code class="keyw">false</code>, <var class="Arg">arg3</var> is a function returning a list or collection, and <var class="Arg">arg4</var> is another function returning <code class="keyw">true</code> or <code class="keyw">false</code>, then</p>
2707
2708 <p><code class="code"><var class="Arg">result</var> := ListX( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">arg3</var>, <var class="Arg">arg4</var>, <var class="Arg">func</var> );</code></p>
2709
2710 <p>is equivalent to</p>
2711
2712
2713 <div class="example"><pre>
2714 result := [];
2715 for v1 in arg1 do
2716 if arg2( v1 ) then
2717 for v2 in arg3( v1 ) do
2718 if arg4( v1, v2 ) then
2719 Add( result, func( v1, v2 ) );
2720 fi;
2721 od;
2722 fi;
2723 od;
2724 </pre></div>
2725
2726 <p>The following example shows how <code class="func">ListX</code> can be used to compute all pairs and all strictly sorted pairs of elements in a list.</p>
2727
2728
2729 <div class="example"><pre>
2730 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [ 1, 2, 3, 4 ];;</span>
2731 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">pair:= function( x, y ) return [ x, y ]; end;;</span>
2732 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ListX( l, l, {x,y} -&gt; [x,y] );</span>
2733 [ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 1 ], [ 2, 2 ],
2734 [ 2, 3 ], [ 2, 4 ], [ 3, 1 ], [ 3, 2 ], [ 3, 3 ], [ 3, 4 ],
2735 [ 4, 1 ], [ 4, 2 ], [ 4, 3 ], [ 4, 4 ] ]
2736 </pre></div>
2737
2738 <p>In the following example, <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>) is the comparison operation:</p>
2739
2740
2741 <div class="example"><pre>
2742 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ListX( l, l, \&lt;, pair );</span>
2743 [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ] ]
2744 </pre></div>
2745
2746 <p><a id="X7AC321B87A2DCAF5" name="X7AC321B87A2DCAF5"></a></p>
2747
2748 <h5>21.21-4 SetX</h5>
2749
2750 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SetX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2751 <p>The only difference between <code class="func">SetX</code> and <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) is that the result list of <code class="func">SetX</code> is strictly sorted.</p>
2752
2753
2754 <div class="example"><pre>
2755 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SetX( [ 1 .. 3 ], [ 1 .. 3 ], \+ );</span>
2756 [ 2, 3, 4, 5, 6 ]
2757 </pre></div>
2758
2759 <p><a id="X82B1411E7FBE925F" name="X82B1411E7FBE925F"></a></p>
2760
2761 <h5>21.21-5 SumX</h5>
2762
2763 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SumX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2764 <p><code class="func">SumX</code> returns the sum of the elements in the list obtained by <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) when this is called with the same arguments.</p>
2765
2766
2767 <div class="example"><pre>
2768 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SumX( [ 1 .. 3 ], [ 1 .. 3 ], \+ );</span>
2769 36
2770 </pre></div>
2771
2772 <p><a id="X7FB318B47D8783DA" name="X7FB318B47D8783DA"></a></p>
2773
2774 <h5>21.21-6 ProductX</h5>
2775
2776 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ProductX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2777 <p><code class="func">ProductX</code> returns the product of the elements in the list obtained by <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) when this is called with the same arguments.</p>
2778
2779
2780 <div class="example"><pre>
2781 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ProductX( [ 1 .. 3 ], [ 1 .. 3 ], \+ );</span>
2782 172800
2783 </pre></div>
2784
2785 <p><a id="X86CF6FC87C5C9A08" name="X86CF6FC87C5C9A08"></a></p>
2786
2787 <h5>21.21-7 ForAllX</h5>
2788
2789 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ForAllX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2790 <p><code class="func">ForAllX</code> returns <code class="keyw">true</code> if all elements are <code class="keyw">true</code> in the list obtained by calling <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) with the same arguments. Otherwise <code class="keyw">false</code> is returned. As with <code class="func">ForAll</code> (<a href="chap21.html#X7F06961278166671"><span class="RefLink">21.20-23</span></a>), the last argument must return either <code class="keyw">true</code> or <code class="keyw">false</code>, and evaluation stops as soon as the result is known.</p>
2791
2792
2793 <div class="example"><pre>
2794 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAllX( [ 1 .. 3 ], [ 1 .. 3 ], \&lt; );</span>
2795 false
2796 </pre></div>
2797
2798 <p><a id="X858C7CEF7EC1260C" name="X858C7CEF7EC1260C"></a></p>
2799
2800 <h5>21.21-8 ForAnyX</h5>
2801
2802 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ForAnyX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2803 <p><code class="func">ForAnyX</code> returns <code class="keyw">true</code> if any element is <code class="keyw">true</code> in the list obtained by calling <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) with the same arguments. Otherwise <code class="keyw">false</code> is returned. As with <code class="func">ForAny</code> (<a href="chap21.html#X7AF82E747A8BDA75"><span class="RefLink">21.20-24</span></a>), the last argument must return either <code class="keyw">true</code> or <code class="keyw">false</code>, and evaluation stops as soon as the result is known.</p>
2804
2805
2806 <div class="example"><pre>
2807 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAnyX( [ 1 .. 3 ], [ 1 .. 3 ], \&lt; );</span>
2808 true
2809 </pre></div>
2810
2811 <p><a id="X877CF5DE7AE12506" name="X877CF5DE7AE12506"></a></p>
2812
2813 <h5>21.21-9 FilteredX</h5>
2814
2815 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FilteredX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2816 <p><code class="func">FilteredX</code> returns the tuples of loop-variable values for which the last argument returns <code class="keyw">true</code>. In other words, it keeps precisely those tuples that would pass the filters when calling <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) with the same arguments.</p>
2817
2818 <p>Even with only one generator, the result consists of singleton tuples. Thus <code class="code">FilteredX( [ 1 .. 4 ], IsEvenInt )</code> returns <code class="code">[ [ 2 ], [ 4 ] ]</code>, not <code class="code">[ 2, 4 ]</code>. Use <code class="func">Filtered</code> (<a href="chap21.html#X7C86D7F7795125F0"><span class="RefLink">21.20-19</span></a>) if you want to filter the elements of a single list or collection directly.</p>
2819
2820
2821 <div class="example"><pre>
2822 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FilteredX( [ 1 .. 4 ], IsEvenInt );</span>
2823 [ [ 2 ], [ 4 ] ]
2824 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FilteredX( [ 1 .. 4 ], [ 1 .. 4 ], \&lt; );</span>
2825 [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ] ]
2826 </pre></div>
2827
2828 <p><a id="X7D6C138F84A3C985" name="X7D6C138F84A3C985"></a></p>
2829
2830 <h5>21.21-10 NumberX</h5>
2831
2832 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NumberX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2833 <p><code class="func">NumberX</code> returns the number of tuples selected by the last argument, using the same arguments as <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>). Equivalently, it counts the entries of the result that <code class="func">FilteredX</code> (<a href="chap21.html#X877CF5DE7AE12506"><span class="RefLink">21.21-9</span></a>) would return with the same arguments.</p>
2834
2835
2836 <div class="example"><pre>
2837 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NumberX( [ 1 .. 4 ], IsEvenInt );</span>
2838 2
2839 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NumberX( [ 1 .. 4 ], [ 1 .. 4 ], \&lt; );</span>
2840 6
2841 </pre></div>
2842
2843 <p><a id="X82580BC887390C69" name="X82580BC887390C69"></a></p>
2844
2845 <h5>21.21-11 PerformX</h5>
2846
2847 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PerformX</code>( <var class="Arg">arg1</var>, <var class="Arg">arg2</var>, <var class="Arg">...</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2848 <p><code class="func">PerformX</code> works like <code class="func">ListX</code> (<a href="chap21.html#X8258477D7F72171B"><span class="RefLink">21.21-3</span></a>) except that it returns nothing and ignores the return values of <var class="Arg">func</var>. It is useful for iterating through the tuples described by the arguments purely for their side effects.</p>
2849
2850
2851 <div class="example"><pre>
2852 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PerformX( [ 1 .. 2 ], [ 3 .. 4 ], Print ); Print( "\n" );</span>
2853 13142324
2854 </pre></div>
2855
2856 <p><a id="X79596BDE7CAF8491" name="X79596BDE7CAF8491"></a></p>
2857
2858 <h4>21.22 <span class="Heading">Ranges</span></h4>
2859
2860 <p>A <em>range</em> is a dense list of integers in arithmetic progression (or degression). This is a list of integers such that the difference between consecutive elements is a nonzero constant. Ranges can be abbreviated with the syntactic construct</p>
2861
2862 <p><code class="code">[ <var class="Arg">first</var>, <var class="Arg">second</var> .. <var class="Arg">last</var> ]</code></p>
2863
2864 <p>or, if the difference between consecutive elements is 1, as</p>
2865
2866 <p><code class="code">[ <var class="Arg">first</var> .. <var class="Arg">last</var> ]</code>.</p>
2867
2868 <p>If <code class="code"><var class="Arg">first</var> &gt; <var class="Arg">last</var></code>, <code class="code">[ <var class="Arg">first</var> .. <var class="Arg">last</var> ]</code> is the empty list, which by definition is also a range; also, if <code class="code"><var class="Arg">second</var> &gt; <var class="Arg">first</var> &gt; <var class="Arg">last</var></code> or <code class="code"><var class="Arg">second</var> &lt; <var class="Arg">first</var> &lt; <var class="Arg">last</var></code>, then <code class="code">[ <var class="Arg">first</var>, <var class="Arg">second</var> .. <var class="Arg">last</var> ]</code> is the empty list. If <code class="code"><var class="Arg">first</var> = <var class="Arg">last</var></code>, <code class="code">[ <var class="Arg">first</var>, <var class="Arg">second</var> .. <var class="Arg">last</var> ]</code> is a singleton list, which is a range, too. Note that <code class="code"><var class="Arg">last</var> - <var class="Arg">first</var></code> must be divisible by the increment <code class="code"><var class="Arg">second</var> - <var class="Arg">first</var></code>, otherwise an error is signalled.</p>
2869
2870 <p>Currently, the integers <var class="Arg">first</var>, <var class="Arg">second</var> and <var class="Arg">last</var> and the length of a range must be <em>small integers</em> as defined in chapter <a href="chap14.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>.</p>
2871
2872 <p>Note also that a range is just a special case of a list. Thus you can access elements in a range (see <a href="chap21.html#X7921047F83F5FA28"><span class="RefLink">21.3</span></a>), test for membership etc. You can even assign to such a range if it is mutable (see <a href="chap21.html#X8611EF768210625B"><span class="RefLink">21.4</span></a>). Of course, unless you assign <code class="code"><var class="Arg">last</var> + <var class="Arg">second</var> - <var class="Arg">first</var></code> to the entry <code class="code"><var class="Arg">range</var>[ Length( <var class="Arg">range</var> ) + 1 ]</code>, the resulting list will no longer be a range. Note that assigning to an entry of <var class="Arg">range</var> will convert it back into a plain list.</p>
2873
2874
2875 <div class="example"><pre>
2876 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := [10..20];</span>
2877 [ 10 .. 20 ]
2878 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length( r );</span>
2879 11
2880 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r[3];</span>
2881 12
2882 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">17 in r;</span>
2883 true
2884 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># r still is a range but is now represented as a plain list</span>
2885 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r[1] := 10;; r;</span>
2886 [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
2887 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange(r);</span>
2888 true
2889 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># r is no longer a range</span>
2890 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r[12] := 25;;</span>
2891 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange(r);</span>
2892 false
2893 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := [1,3..17];</span>
2894 [ 1, 3 .. 17 ]
2895 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Length( r );</span>
2896 9
2897 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r[4];</span>
2898 7
2899 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := [0,-1..-9];</span>
2900 [ 0, -1 .. -9 ]
2901 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r[5];</span>
2902 -4
2903 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r := [ 1, 4 .. 32 ];</span>
2904 Error, Range: &lt;last&gt;-&lt;first&gt; (31) must be divisible by &lt;inc&gt; (3)
2905 </pre></div>
2906
2907 <p>Most often ranges are used in connection with the <code class="keyw">for</code>-loop see <a href="chap4.html#X78783E777867638A"><span class="RefLink">4.15-6</span></a>). Here the construct</p>
2908
2909 <p><code class="code">for <var class="Arg">var</var> in [ <var class="Arg">first</var> .. <var class="Arg">last</var> ] do <var class="Arg">statements</var> od</code></p>
2910
2911 <p>replaces the</p>
2912
2913 <p><code class="code">for <var class="Arg">var</var> from <var class="Arg">first</var> to <var class="Arg">last</var> do <var class="Arg">statements</var> od</code></p>
2914
2915 <p>which is more usual in other programming languages.</p>
2916
2917
2918 <div class="example"><pre>
2919 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">s := [];; for i in [10..20] do Add( s, i^2 ); od; s;</span>
2920 [ 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400 ]
2921 </pre></div>
2922
2923 <p>Note that a range with <code class="code"><var class="Arg">last</var> &gt;= <var class="Arg">first</var></code> is at the same time also a proper set (see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>), because it contains no holes or duplicates and is sorted, and also a row vector (see <a href="chap23.html#X82C7E6CF7BA03391"><span class="RefLink">23</span></a>), because it contains no holes and all elements are integers.</p>
2924
2925 <p><a id="X86DDC2FF7A50FBEE" name="X86DDC2FF7A50FBEE"></a></p>
2926
2927 <h5>21.22-1 IsRange</h5>
2928
2929 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsRange</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
2930 <p>tests if the object <var class="Arg">obj</var> is a range, i.e. is a dense list of integers that is also a range (see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a> for a definition of <q>range</q>).</p>
2931
2932
2933 <div class="example"><pre>
2934 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange( [1,2,3] ); IsRange( [7,5,3,1] );</span>
2935 true
2936 true
2937 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange( [1 .. 3] );</span>
2938 true
2939 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange( [1,2,4,5] ); IsRange( [1,,3,,5,,7] );</span>
2940 false
2941 false
2942 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRange( [] ); IsRange( [1] );</span>
2943 true
2944 true
2945 </pre></div>
2946
2947 <p><a id="X83896BC481536B07" name="X83896BC481536B07"></a></p>
2948
2949 <h5>21.22-2 IsRangeRep</h5>
2950
2951 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsRangeRep</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;representation&nbsp;)</td></tr></table></div>
2952 <p>Tests whether <var class="Arg">obj</var> is represented as a range, that is by internally storing only the first value, the in- or decrement, and the last value of the range.</p>
2953
2954 <p>To test whether a list is a range in the mathematical sense see <code class="func">IsRange</code> (<a href="chap21.html#X86DDC2FF7A50FBEE"><span class="RefLink">21.22-1</span></a>).</p>
2955
2956 <p>Lists created by the syntactic construct <code class="code">[ <var class="Arg">first</var>, <var class="Arg">second</var> .. <var class="Arg">last</var> ]</code>, see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a>, are in <code class="func">IsRangeRep</code>.</p>
2957
2958 <p>Note that if you modify an <code class="func">IsRangeRep</code> object by assigning to one of its entries, or by using <code class="func">Add</code> (<a href="chap21.html#X795EC9D67E34DAB0"><span class="RefLink">21.4-2</span></a>) or <code class="func">Append</code> (<a href="chap21.html#X79E31DB27C82D6E1"><span class="RefLink">21.4-5</span></a>), then the range may be converted into a plain list, even though the resulting list may still be a range, mathematically.</p>
2959
2960
2961 <div class="example"><pre>
2962 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRangeRep( [1 .. 3] );</span>
2963 true
2964 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsRangeRep( [1, 2, 3] );</span>
2965 false
2966 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l := [1..3];;</span>
2967 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l[1] := 1;;</span>
2968 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l;</span>
2969 [ 1, 2, 3 ]
2970 </pre></div>
2971
2972 <p><a id="X7D22B2298167A58F" name="X7D22B2298167A58F"></a></p>
2973
2974 <h5>21.22-3 ConvertToRangeRep</h5>
2975
2976 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ConvertToRangeRep</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
2977 <p>For some lists the <strong class="pkg">GAP</strong> kernel knows that they are in fact ranges. Those lists are represented internally in a compact way, namely in <code class="func">IsRangeRep</code> (<a href="chap21.html#X83896BC481536B07"><span class="RefLink">21.22-2</span></a>), instead of as plain lists. A list that is represented as a plain list might still be a range but <strong class="pkg">GAP</strong> may not know this.</p>
2978
2979 <p>If <var class="Arg">list</var> is a range then <code class="func">ConvertToRangeRep</code> changes the representation of <var class="Arg">list</var> to <code class="func">IsRangeRep</code> (<a href="chap21.html#X83896BC481536B07"><span class="RefLink">21.22-2</span></a>). A call of <code class="func">ConvertToRangeRep</code> for a list that is not a range is ignored.</p>
2980
2981
2982 <div class="example"><pre>
2983 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r:= [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];</span>
2984 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
2985 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ConvertToRangeRep( r ); r;</span>
2986 [ 1 .. 10 ]
2987 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [ 1, 2, 4, 5 ];; ConvertToRangeRep( l ); l;</span>
2988 [ 1, 2, 4, 5 ]
2989 </pre></div>
2990
2991 <p><a id="X7EA3ACE27E43D174" name="X7EA3ACE27E43D174"></a></p>
2992
2993 <h4>21.23 <span class="Heading">Enumerators</span></h4>
2994
2995 <p>An <em>enumerator</em> is an immutable list that need not store its elements explicitly but knows, from a set of basic data, how to determine the <span class="SimpleMath">i</span>-th element and the position of a given object. A typical example of this is a vector space over a finite field with <span class="SimpleMath">q</span> elements for which it is very easy to enumerate all elements using <span class="SimpleMath">q</span>-adic expansions of integers.</p>
2996
2997 <p>Using this enumeration can be even quicker than a binary search in a sorted list of vectors, see <code class="func">IsQuickPositionList</code> (<a href="chap21.html#X7BB462C17962647F"><span class="RefLink">21.23-1</span></a>).</p>
2998
2999 <p>On the one hand, element access to an enumerator may take more time than element access to an internally represented list containing the same elements. On the other hand, an enumerator may save a vast amount of memory. Take for example a permutation group of size a few millions. Even for moderate degree it is unlikely that a list of all its elements will fit into memory whereas it is no problem to construct an enumerator from a stabilizer chain (see <a href="chap43.html#X7FA58C3A8283F3BD"><span class="RefLink">43.6</span></a>).</p>
3000
3001 <p>There are situations where one only wants to loop over the elements of a domain, without using the special facilities of an enumerator, namely the particular order of elements and the possibility to find the position of elements. For such cases, <strong class="pkg">GAP</strong> provides iterators (see <a href="chap30.html#X85A3F00985453F95"><span class="RefLink">30.8</span></a>).</p>
3002
3003 <p>The functions <code class="func">Enumerator</code> (<a href="chap30.html#X7EF8910F82B45EC7"><span class="RefLink">30.3-2</span></a>) and <code class="func">EnumeratorSorted</code> (<a href="chap30.html#X80CD7DDC7D0C60D5"><span class="RefLink">30.3-3</span></a>) return enumerators of domains. Most of the special implementations of enumerators in the <strong class="pkg">GAP</strong> library are based on the general interface that is provided by <code class="func">EnumeratorByFunctions</code> (<a href="chap30.html#X85E149177AC547C3"><span class="RefLink">30.3-4</span></a>); one generic example is <code class="func">EnumeratorByBasis</code> (<a href="chap61.html#X7EB0D16A7EC2DEE3"><span class="RefLink">61.6-5</span></a>), which can be used to get an enumerator of a finite dimensional free module.</p>
3004
3005 <p>Also enumerators for non-domains can be implemented via <code class="func">EnumeratorByFunctions</code> (<a href="chap30.html#X85E149177AC547C3"><span class="RefLink">30.3-4</span></a>); for a discussion, see <a href="chap79.html#X849D8BC278649EA5"><span class="RefLink">79.5</span></a>.</p>
3006
3007 <p><a id="X7BB462C17962647F" name="X7BB462C17962647F"></a></p>
3008
3009 <h5>21.23-1 IsQuickPositionList</h5>
3010
3011 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsQuickPositionList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;filter&nbsp;)</td></tr></table></div>
3012 <p>This filter indicates that a position test in <var class="Arg">list</var> is quicker than about 5 or 6 element comparisons for <q>smaller</q>. If this is the case it can be beneficial to use <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) in <var class="Arg">list</var> and a bit list than ordered lists to represent subsets of <var class="Arg">list</var>.</p>
3013
3014 <p><a id="X81ECC2077D88E112" name="X81ECC2077D88E112"></a></p>
3015
3016 <h4>21.24 <span class="Heading">Plain Lists</span></h4>
3017
3018 <p>Plain lists are the default kind of lists in <strong class="pkg">GAP</strong>, in the sense that <strong class="pkg">GAP</strong> stores the list entries and does not know how to do better (as opposed to ranges or strings, which are also lists). Often it is not necessary to know how a given list is represented internally, the operations defined for lists apply to all lists.</p>
3019
3020 <p>Typical situations where the representation matters are when one wants to make sure that the given list is <em>not</em> a plain list and thus will be handled more efficiently, for example when one installs a method for a particular operation, where an argument is required to be a list in a particular representation.</p>
3021
3022 <p><a id="X8438CB908367254C" name="X8438CB908367254C"></a></p>
3023
3024 <h5>21.24-1 PlainListCopy</h5>
3025
3026 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PlainListCopy</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
3027 <p>This function returns a list equal to its argument, in a plain list representation (see <code class="func">IsPlistRep</code> (<a href="chap21.html#X87BA4EBF80F16B72"><span class="RefLink">21.24-2</span></a>)). This is intended for use in certain rare situations, such as before objectifying, or calling some kernel functions.</p>
3028
3029 <p><a id="X87BA4EBF80F16B72" name="X87BA4EBF80F16B72"></a></p>
3030
3031 <h5>21.24-2 IsPlistRep</h5>
3032
3033 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPlistRep</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;representation&nbsp;)</td></tr></table></div>
3034 <p><strong class="pkg">GAP</strong> lists created by entering comma separated values in square brackets are usually represented internally as so-called <em>plain lists</em>. Other representations of lists are <code class="func">IsBlistRep</code> (<a href="chap22.html#X8453ADDA810B4C03"><span class="RefLink">22.5-1</span></a>), <code class="func">IsRangeRep</code> (<a href="chap21.html#X83896BC481536B07"><span class="RefLink">21.22-2</span></a>), <code class="func">IsStringRep</code> (<a href="chap27.html#X7A17EDF8785C9F58"><span class="RefLink">27.4-1</span></a>), or the ones that are chosen for implementing enumerators, see Section <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a>.</p>
3035
3036
3037 <div class="example"><pre>
3038 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPlistRep( [ 1, 2, 3 ] );</span>
3039 true
3040 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPlistRep( "abc" );</span>
3041 false
3042 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPlistRep( [ 1 .. 5 ] );</span>
3043 false
3044 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPlistRep( BlistList( [ 1 .. 5 ], [ 1 ] ) );</span>
3045 false
3046 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPlistRep( 0 );</span>
3047 false
3048 </pre></div>
3049
3050
3051 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap20.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap22.html">[Next Chapter]</a>&nbsp; </div>
3052
3053
3054 <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="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
3055
3056 <hr />
3057 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
3058 </body>
3059 </html>