Home Contents Index Summary Previous Next

3.26 List Manipulation

is_list(+Term)
Succeeds if Term is bound to the empty list () or a term with functor `.' and arity 2.

proper_list(+Term)
Equivalent to is_list/1, but also requires the tail of the list to be a list (recursively). Examples:


is_list([x|A])          % true
proper_list([x|A])      % false

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).

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.

memberchk(?Elem, +List)
Equivalent to member/2, but leaves no choice point.

delete(+List1, ?Elem, ?List2)
Delete all members of List1 that simultaneously unify with Elem and unify the result with List2.

select(?List1, ?Elem, ?List2)
Select an element of List1 that unifies with Elem. List2 is unified with the list remaining from List1 after deleting the selected element. Normally used with the instantiation pattern +List1, -Elem, -List2, but can also be used to insert an element in a list using -List1, +Elem, +List2.

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(?Elem, ?List)
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.

reverse(+List1, -List2)
Reverse the order of the elements in List1 and unify the result with the elements of List2.

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]

length(?List, ?Int)
Succeeds if Int represents the number of elements of list List. Can be used to create a list holding only variables.

merge(+List1, +List2, -List3)
List1 and List2 are lists, sorted to the standard order of terms (see section 3.5). List3 will be unified with an ordered list holding both the elements of List1 and List2. Duplicates are not removed.