Did you know ... | Search Documentation: |
library(lists): List Manipulation |
library(lists)
, but the
set of provided predicates is diverse. There is a fair agreement on the
semantics of most of these predicates, although error handling may vary.This library provides commonly accepted basic predicates for list manipulation in the Prolog community. Some additional list manipulations are built-in. See e.g., memberchk/2, length/2.
The implementation of this library is copied from many places. These include: "The Craft of Prolog", the DEC-10 Prolog library (LISTRO.PL) and the YAP lists library. Some predicates are reimplemented based on their specification by Quintus and SICStus.
member(X, [One]).
ListOfLists | must be a list of possibly partial lists |
append(Part, _, Whole)
.?- select(b, [a,b,c,b], 2, X). X = [a, 2, c, b] ; X = [a, b, c, 2] ; false.
\+ Elem \= H
, which implies that Elem is
not changed.
==
),
delete first/all, be deterministic or not.type_error(integer, Index)
if Index is not an
integer or unbound.?- nth0(I, [a,b,c], E, R). I = 0, E = a, R = [b, c] ; I = 1, E = b, R = [a, c] ; I = 2, E = c, R = [a, b] ; false.
?- nth0(1, L, a1, [a,b]). L = [a, a1, b].
semidet
if List is a list and multi
if List is a partial list.
append(_, [Last], List)
as a portable alternative.proper_length(List, Length) :- is_list(List), length(List, Length).
If both Xs and Ys are provided and both lists
have equal length the order is |
Xs|
^
2.
Simply testing whether Xs is a permutation of Ys
can be achieved in order log(|
Xs|
)
using msort/2 as
illustrated below with the semidet
predicate is_permutation/2:
is_permutation(Xs, Ys) :- msort(Xs, Sorted), msort(Ys, Sorted).
The example below illustrates that Xs and Ys being proper lists is not a sufficient condition to use the above replacement.
?- permutation([1,2], [X,Y]). X = 1, Y = 2 ; X = 2, Y = 1 ; false.
type_error(list, Arg)
if either argument is not a proper or
partial list.[]
’is removed too. In SWI7, []
is distinct from’[]
’.
Ending up needing flatten/2 often indicates, like append/3 for appending two lists, a bad design. Efficient code that generates lists from generated small lists must use difference lists, often possible through grammar rules for optimal readability.
Item-Count
pairs that
represents the run length encoding of Items. For
example:
?- clumped([a,a,b,a,a,a,a,c,c,c], R). R = [a-2, b-1, a-4, c-3].
@=<
)/2.
Fails if List is empty. The following call is equivalent to max_member/2:
?- max_member(@=<, X, [6,1,8,4]). X = 8.
@=<
)/2.
Fails if List is empty. The following call is equivalent to max_member/2:
?- min_member(@=<, X, [6,1,8,4]). X = 1.
type_error(integer, Low)
type_error(integer, High)
log(N)
and the
predicate may cause a resource-error. There are no other error
conditions.==
E2 holds. The complexity
of the implementation is N*log(N)
.
**
2. The list_to_set/2
predicate is more expensive than sort/2
because it involves, two sorts and a linear scan.**
2 and equality was tested using =/2.|
Set1|
*|
Set2|
.
A set is defined to be an unordered list without duplicates.
Elements are considered duplicates if they can be unified.
|
Set1|
*|
Set2|
.
A set is defined to be an unordered list without duplicates.
Elements are considered duplicates if they can be unified.
|
SubSet|
*|
Set|
.
A
set is defined to be an unordered list without duplicates.
Elements are considered duplicates if they can be unified.
|
Delete|
*|
Set|
.
A
set is defined to be an unordered list without duplicates.
Elements are considered duplicates if they can be unified.
I would also point the user to
https://www.swi-prolog.org/pldoc/man?section=ordsets
which provides predicates working on "ordered sets", with better efficiency I suppose,
as an alternative.
The predicate transpose/2 from library(clpfd)
should probably rather be here than there.