## A.12 library( lists ): List Manipulation

This library provides commonly accepted basic predicates for list manipulation in the Prolog community. Some additional list manipulations are built-in. Their description is in section 4.29.

append(?List1, ?List2, ?List3)
  Succeeds when List3 unifies with the concatenation of List1 and List2. The predicate can be used with any instantiation pattern (even three variables).
append(?ListOfLists, ?List)
  Concatenate a list of lists. Is true if Lists is a list of lists, and List is the concatenation of these lists. ListOfLists must be a list of -possibly- partial lists.
member(?Elem, ?List)
  Succeeds when Elem can be unified with one of the members of List. The predicate can be used with any instantiation pattern.
nextto(?X, ?Y, ?List)
  Succeeds when Y immediatly follows X in List.
delete(+List1, ?Elem, ?List2)
  Delete all members of List1 that simultaneously unify with Elem and unify the result with List2.
select(?Elem, ?List, ?Rest)
  Select Elem from List leaving Rest. It behaves as member/2, returning the remaining elements in Rest. Note that besides selecting elements from a list, it can also be used to insert elements.
selectchk(?Elem, ?List, ?Rest)
  Semi-deterministic selection from a list. Steadfast: defines as

      selectchk(Elem, List, Rest) :-
              select(Elem, List, Rest0), !,
              Rest = Rest0.

nth0(?Index, ?List, ?Elem)
  Succeeds when the Index-th element of List unifies with Elem. Counting starts at 0.
nth1(?Index, ?List, ?Elem)
  Succeeds when the Index-th element of List unifies with Elem. Counting starts at 1.
last(?List, ?Elem)
  Succeeds if Elem unifies with the last element of List. If List is a proper list last/2 is deterministic. If List has an unbound tail, backtracking will cause List to grow.83The argument order of this predicate was changed in 5.1.12 for compatibility reasons.
reverse(+List1, -List2)
  Reverse the order of the elements in List1 and unify the result with the elements of List2.
permutation(?List1, ?List2)
  Permuation is true when List1 is a permutation of List2. The implementation can solve for List2 given List1 or List1 given List2, or even enumerate List1 and List2 together.
flatten(+List1, -List2)
  Transform List1, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively). Unify the resulting flat list with List2. Example:

      ?- flatten([a, [b, [c, d], e]], X).

      X = [a, b, c, d, e]

sumlist(+List, -Sum)
  Unify Sum to the result of adding all elements in List. List must be a proper list holding numbers. See number/1 and is/2. for details on arithmetic.
max_list(+List, -Max)
  True if Max is the largest number in List. See also the function max/2.
min_list(+List, -Min)
  True if Min is the smallest number in List. See also the function min/2.
numlist(+Low, +High, -List)
  If Low and High are integers with Low =< High, unify List to a list [Low, Low+1, ...High]. See also between/3.

### A.12.1 Set Manipulation

The set predicates listed in this section work on ordinary unsorted lists. Note that this makes many of the operations order N^2. For larger sets consider the use of ordered sets as implemented by library `ordsets.pl`, running most these operations in order N. See section A.16.

is_set(+Set)
  Succeeds if Set is a list (see is_list/1) without duplicates.
list_to_set(+List, -Set)
  Unifies Set with a list holding the same elements as List in the same order. If list contains duplicates, only the first is retained. See also sort/2. Example:

      ?- list_to_set([a,b,a], X)

      X = [a,b]

intersection(+Set1, +Set2, -Set3)
  Succeeds if Set3 unifies with the intersection of Set1 and Set2. Set1 and Set2 are lists without duplicates. They need not be ordered.
subtract(+Set, +Delete, -Result)
  Delete all elements of set `Delete' from `Set' and unify the resulting set with `Result'.
union(+Set1, +Set2, -Set3)
  Succeeds if Set3 unifies with the union of Set1 and Set2. Set1 and Set2 are lists without duplicates. They need not be ordered.
subset(+Subset, +Set)
  Succeeds if all elements of Subset are elements of Set as well.