File: gprolog043.html

package info (click to toggle)
gprolog 1.3.0-6.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 13,512 kB
  • ctags: 8,954
  • sloc: ansic: 57,431; perl: 16,620; sh: 5,900; makefile: 1,284
file content (389 lines) | stat: -rw-r--r-- 12,410 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="gprolog.css">
<TITLE>
List processing
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog042.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog044.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H3 CLASS="subsection"><A NAME="htoc200">7.20</A>&nbsp;&nbsp;List processing</H3><UL>
<LI><A HREF="gprolog043.html#toc159"><TT>append/3</TT></A>
<LI><A HREF="gprolog043.html#toc160"><TT>member/2</TT>,
 <TT>memberchk/2</TT></A>
<LI><A HREF="gprolog043.html#toc161"><TT>reverse/2</TT></A>
<LI><A HREF="gprolog043.html#toc162"><TT>delete/3</TT>,
 <TT>select/3</TT></A>
<LI><A HREF="gprolog043.html#toc163"><TT>permutation/2</TT></A>
<LI><A HREF="gprolog043.html#toc164"><TT>prefix/2</TT>,
 <TT>suffix/2</TT></A>
<LI><A HREF="gprolog043.html#toc165"><TT>sublist/2</TT></A>
<LI><A HREF="gprolog043.html#toc166"><TT>last/2</TT></A>
<LI><A HREF="gprolog043.html#toc167"><TT>length/2</TT></A>
<LI><A HREF="gprolog043.html#toc168"><TT>nth/3</TT></A>
<LI><A HREF="gprolog043.html#toc169"><TT>max_list/2</TT>,
 <TT>min_list/2</TT>,
 <TT>sum_list/2</TT></A>
<LI><A HREF="gprolog043.html#toc170"><TT>sort/2</TT>,
 <TT>sort0/2</TT>,
 <TT>keysort/2</TT>
 <TT>sort/1</TT>,
 <TT>sort0/1</TT>,
 <TT>keysort/1</TT></A>
</UL>

These predicates manipulate lists. They are bootstrapped predicates (i.e.
written in Prolog) and no error cases are tested (for the moment). However,
since they are written in Prolog using other built-in predicates, some
errors can occur due to those built-in predicates.<BR>
<BR>
<A NAME="toc159"></A>
<H4 CLASS="subsubsection"><A NAME="htoc201">7.20.1</A>&nbsp;&nbsp;<TT>append/3</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
append(?list, ?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>append(List1, List2, List12)</TT> succeeds if the concatenation of the
list <TT>List1</TT> and the list <TT>List2</TT> is the list <TT>List12</TT>.
This predicate is re-executable on backtracking (e.g. if <TT>List12</TT> is
instantiated and both <TT>List1</TT> and <TT>List2</TT> are variable).<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc160"></A>
<H4 CLASS="subsubsection"><A NAME="htoc202">7.20.2</A>&nbsp;&nbsp;<TT>member/2</TT>,
 <TT>memberchk/2</TT></H4>
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
member(?term, ?list)<BR>
memberchk(?term, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>member(Element, List)</TT> succeeds if <TT>Element</TT> belongs to the
<TT>List</TT>. This predicate is re-executable on backtracking and can be
thus used to enumerate the elements of <TT>List</TT>.<BR>
<BR>
<TT>memberchk/2</TT> is similar to <TT>member/2</TT> but only succeeds once.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc161"></A>
<H4 CLASS="subsubsection"><A NAME="htoc203">7.20.3</A>&nbsp;&nbsp;<TT>reverse/2</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
reverse(?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>reverse(List1, List2)</TT> succeeds if <TT>List2</TT> unifies with the
list <TT>List1</TT> in reverse order.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc162"></A>
<H4 CLASS="subsubsection"><A NAME="htoc204">7.20.4</A>&nbsp;&nbsp;<TT>delete/3</TT>,
 <TT>select/3</TT></H4>
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
delete(?list, ?term, ?list)<BR>
select(?term, ?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>delete(List1, Element, List2)</TT> removes all occurrences of
<TT>Element</TT> in <TT>List1</TT> to provide <TT>List2</TT>. A strict term
equality is required, cf. <TT>(==)/2</TT> (section&nbsp;<A HREF="gprolog026.html#(==)/2">7.3.2</A>).<BR>
<BR>
<TT>select(Element, List1, List2)</TT> removes one occurrence of
<TT>Element</TT> in <TT>List1</TT> to provide <TT>List2</TT>. This predicate
is re-executable on backtracking.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc163"></A>
<H4 CLASS="subsubsection"><A NAME="htoc205">7.20.5</A>&nbsp;&nbsp;<TT>permutation/2</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
permutation(?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>permutation(List1, List2)</TT> succeeds if <TT>List2</TT>
is a permutation of the elements of <TT>List1</TT>. This predicate is
re-executable on backtracking.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc164"></A>
<H4 CLASS="subsubsection"><A NAME="htoc206">7.20.6</A>&nbsp;&nbsp;<TT>prefix/2</TT>,
 <TT>suffix/2</TT></H4>
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
prefix(?list, ?list)<BR>
suffix(?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>prefix(Prefix, List)</TT> succeeds if <TT>Prefix</TT> is a prefix of
<TT>List</TT>. This predicate is re-executable on backtracking.<BR>
<BR>
<TT>suffix(Suffix, List)</TT> succeeds if <TT>Suffix</TT> is a suffix of
<TT>List</TT>. This predicate is re-executable on backtracking.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc165"></A>
<H4 CLASS="subsubsection"><A NAME="htoc207">7.20.7</A>&nbsp;&nbsp;<TT>sublist/2</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
sublist(?list, ?list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>sublist(List1, List2)</TT> succeeds if <TT>List1</TT> is a sub-list of
<TT>List2</TT>. This predicate is re-executable on backtracking.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc166"></A>
<H4 CLASS="subsubsection"><A NAME="htoc208">7.20.8</A>&nbsp;&nbsp;<TT>last/2</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
last(?list, ?term)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>last(List, Element)</TT> succeeds if <TT>Element</TT> is the last element
of <TT>List</TT>.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc167"></A>
<H4 CLASS="subsubsection"><A NAME="htoc209">7.20.9</A>&nbsp;&nbsp;<TT>length/2</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
length(?list, ?integer)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>length(List, Length)</TT> succeeds if <TT>Length</TT> is the length of
<TT>List</TT>.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc168"></A>
<H4 CLASS="subsubsection"><A NAME="htoc210">7.20.10</A>&nbsp;&nbsp;<TT>nth/3</TT></H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
nth(?integer, ?list, ?term)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>nth(N, List, Element)</TT> succeeds if the <TT>N</TT><EM>th</EM>
argument of <TT>List</TT> is <TT>Element</TT>.<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc169"></A>
<H4 CLASS="subsubsection"><A NAME="htoc211">7.20.11</A>&nbsp;&nbsp;<TT>max_list/2</TT>,
 <TT>min_list/2</TT>,
 <TT>sum_list/2</TT></H4>
 
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
min_list(+list, ?number)<BR>
max_list(+list, ?number)<BR>
sum_list(+list, ?number)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>min_list(List, Min)</TT> succeeds if <TT>Min</TT> is the
smallest number in <TT>List</TT>.<BR>
<BR>
<TT>max_list(List, Max)</TT> succeeds if <TT>Max</TT> is the
largest number in <TT>List</TT>.<BR>
<BR>
<TT>sum_list(List, Sum)</TT> succeeds if <TT>Sum</TT> is the
sum of all the elements in <TT>List</TT>.<BR>
<BR>
<TT>List</TT> must be a list of arithmetic evaluable terms
(section&nbsp;<A HREF="gprolog029.html#Evaluation-of-an-arithmetic-expression">7.6.1</A>).<BR>
<BR>
<B>Errors</B><BR>
<BR>
None.<BR>
<BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicate.<BR>
<BR>
<A NAME="toc170"></A>
<H4 CLASS="subsubsection"><A NAME="htoc212">7.20.12</A>&nbsp;&nbsp;<TT>sort/2</TT>,
 <TT>sort0/2</TT>,
 <TT>keysort/2</TT>
 <TT>sort/1</TT>,
 <TT>sort0/1</TT>,
 <TT>keysort/1</TT></H4>
<A NAME="sort/2"></A>
 
 
 
 
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
sort(+list, ?list)<BR>
sort0(+list, ?list)<BR>
keysort(+list, ?list)<BR>
sort(+list)<BR>
sort0(+list)<BR>
keysort(+list)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>sort(List1, List2)</TT> succeeds if <TT>List2</TT> is the
sorted list corresponding to <TT>List1</TT> where duplicate elements are
merged.<BR>
<BR>
<TT>sort0/2</TT> is similar to <TT>sort/2</TT> except that duplicate elements
are not merged.<BR>
<BR>
<TT>keysort(List1, List2)</TT> succeeds if <TT>List2</TT> is the
sorted list of <TT>List1</TT> according to the keys. The list <TT>List1</TT>
consists of items of the form <TT>Key-Value</TT>. These items are sorted
according to the value of <TT>Key</TT> yielding the <TT>List2</TT>. Duplicate
keys are not merged. This predicate is stable, i.e. if <TT>K-A</TT> occurs
before <TT>K-B</TT> in the input, then <TT>K-A</TT> will occur before
<TT>K-B</TT> in the output.<BR>
<BR>
<TT>sort/1</TT>, <TT>sort0/1</TT> and <TT>keysort/1</TT> are similar to
<TT>sort/2</TT>, <TT>sort0/2</TT> and <TT>keysort/2</TT> but achieve a sort
in-place destructing the original <TT>List1</TT> (this in-place assignment is
not undone at backtracking). The sorted list occupies the same memory space
as the original list (saving thus memory consumption).<BR>
<BR>
The time complexity of these sorts is <I>O</I>(<I>N&nbsp;log&nbsp;N</I>), <I>N</I> being the length of
the list to sort.<BR>
<BR>
These predicates refer to the standard ordering of terms
(section&nbsp;<A HREF="gprolog026.html#Standard-total-ordering-of-terms">7.3.1</A>). <BR>
<BR>
<B>Errors</B><BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>List1</TT> is a partial list</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>List1</TT> is neither a partial list nor a list</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(list, List1)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left><TT>List2</TT> is neither a partial list nor a list</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(list, List2)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR></TABLE><BR>
<B>Portability</B><BR>
<BR>
GNU Prolog predicates.<BR>
<BR>

<HR SIZE=2>
Copyright (C) 1999-2007 Daniel Diaz
<BR>
<BR>
Verbatim copying and distribution of this entire article is permitted in any
medium, provided this notice is preserved. <BR>
<BR>
<A HREF="index.html#copyright">More about the copyright</A>
<HR>
<A HREF="gprolog042.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog044.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>