## 4.29 Built-in list operations

Most list operations are defined in the library `library(lists)` described in section A.12. Some that are implemented with more low-level primitives are built-in and described here.

is_list(+Term)
  True if Term is bound to the empty list (`[]`) or a term with functor ```.``' and arity 2 and the second argument is a list.84In versions before 5.0.1, is_list/1 just checked for `[]` or `[_|_]` and proper_list/1 had the role of the current is_list/1. The current definition conforms to the de facto standard. Assuming proper coding standards, there should only be very few cases where a quick-and-dirty is_list/1 is a good choice. Richard O'Keefe pointed at this issue. This predicate acts as if defined by the definition below on acyclic terms. The implementation fails safely if Term represents a cyclic list.

      is_list(X) :-
              var(X), !,
              fail.
      is_list([]).
      is_list([_|T]) :-
              is_list(T).

memberchk(?Elem, +List)
  Same as `once(member(Elem, List))`. See member/2.
[ISO]length(?List, ?Int)
  True if Int represents the number of elements in List. This predicate is a true relation and can be used to find the length of a list or produce a list (holding variables) of length Int. The predicate is non-deterministic, producing lists of increasing length if List is a partial list and Int is unbound. It raises errors if

  - Int is bound to a non-integer.
  - Int is a negative integer.
  - List is neither a list nor a partial list. This error condition includes cyclic lists.85ISO demands failure here. We think an error is more appropriate.

  This predicate fails if the tail of List is equivalent to Int (e.g., `length(L,L)`).86This is logically correct. An exception would be more appropriate, but to our best knowledge, current practice in Prolog does not describe a suitable candidate exception term.

[ISO]sort(+List, -Sorted)
  True if Sorted can be unified with a list holding the elements of List, sorted to the standard order of terms (see section 4.7). Duplicates are removed. The implementation is in C, using natural merge sort.87Contributed by Richard O'Keefe. The sort/2 predicate can sort a cyclic list, returning a non-cyclic version with the same elements.
msort(+List, -Sorted)
  Equivalent to sort/2, but does not remove duplicates. Raises a `type_error` if List is a cyclic list or not a list.
[ISO]keysort(+List, -Sorted)
  List is a proper list whose elements are `Key-Value`, that is, terms whose principal functor is (-)/2, whose first argument is the sorting key, and whose second argument is the satellite data to be carried along with the key. keysort/2 sorts List like msort/2, but only compares the keys. It is used to sort terms not on standard order, but on any criterion that can be expressed on a multi-dimensional scale. Sorting on more than one criterion can be done using terms as keys, putting the first criterion as argument 1, the second as argument 2, etc. The order of multiple elements that have the same Key is not changed. The implementation is in C, using natural merge sort. Fails with a `type_error` if List is a cyclic list or not a list, or one of the elements of List is not a pair.
predsort(+Pred, +List, -Sorted)
  Sorts similar to sort/2, but determines the order of two terms by calling Pred(-Delta, +E1, +E2) . This call must unify Delta with one of ``<``, ``>`` or ``=``. If the built-in predicate compare/3 is used, the result is the same as sort/2. See also keysort/2.88Please note that the semantics have changed between 3.1.1 and 3.1.2.