Go to the previous, next section.

List Operations

This package defines operations on lists. Lists are a very basic data structure, but nevertheless certain very frequent operations are provided in this package.

To load the package, enter the query

| ?- use_module(library(lists)).

@
Combined is the combined list of the elements in Prefix followed by the elements in Suffix. It can be used to form Combined or it can be used to find Prefix and/or Suffix from a given Combined.

@
Residue is the result of removing all identical occurrences of Element in List.

@
List is a proper list.

@
Last is the last element in List. Example:

| ?- last([x,y,z], Z).

Z = z ? 

yes

@
Max is the largest of the elements in ListOfNumbers.

@
Element is a member of List. It may be used to test for membership in a list, but it can also be used to enumerate all the elements in List. Example:
| ?- member(X, [a,b,c]).

X = a ? ;

X = b ? ;

X = c ? 

yes

@
Element is a member of List, but memberchk/2 only succeeds once and can therefore not be used to enumerate the elements in List. Example:
| ?- memberchk(X, [a,b,c]).

X = a ? ;

no

@
Min is the smallest of the numbers in the list ListOfNumbers.

@
X and Y appears side-by-side in List. Example:
| ?- nextto(X, Y, [1,2,3]).

X = 1,
Y = 2 ? ;

X = 2,
Y = 3 ? ;

no
@
List contains no duplicated elements. This is true when dif(X, Y) holds for all pairs of members X and Y of the list.

@
Element does not occur in List. This is true when dif(Element, Y) holds for all members Y of the list.

@
Element is the Nth element of List. The first element is number 1. Example:
| ?- nth(N, [a,b,c,d,e,f,g,h,i], f).

N = 6 ? 

yes
@
Element is in position N in the List and Rest is all elements in List except Element.

@
Element is the Nth element of List, counting the first element as 0.

@
Element is the Nth element of List, counting the first element as 0. Rest is all the other elements in List. Example:
| ?- nth0(N, [a,b,c,d,e,f,g,h,i,j], f, R).

N = 5,
R = [a,b,c,d,e,g,h,i,j] ? 

yes

@
Perm is a permutation of List.

@
Prefix is a prefix of List. Example:
| ?- prefix([1,2,3], [1,2,3,4,5,6]).

yes

@
Pruned is the result of removing all identical duplicate elements in List. Example:
| ?- remove_duplicates([1,2,3,2,3,1], P).

P = [1,2,3] ? ;

no
@
Reversed has the same elements as List but in a reversed order.

@
List1 and List2 have the same number of elements.

@
List1 and List2 have the same number of elements and that number is Length. Example:
| ?- same_length([1,2,3], [9,8,7], N).

N = 3 ? ;

no

@
The result of removing an occurrence of Element in List is List2.

@
Sub contains some of the elements of List.

@
Xlist and Ylist are equal except for replacing identical occurrences of X by Y. Example:
| ?- substitute(1, [1,2,3,4], 5, X).

X = [5,2,3,4] ? 

yes
@
Sum is the result of adding the ListOfNumbers together.

@
Suffix is a suffix of List.

Go to the previous, next section.