|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.
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.
|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.
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].
semidetif List is a list and
multiif 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
Simply testing whether Xs is a permutation of Ys
can be achieved in order log(
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.
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*
**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.