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)).
     
append(?Prefix, ?Suffix, ?Combined)

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.

delete(+List, +Element, ?Residue)

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

is_list(+List)

List is a proper list.

last(?List, ?Last)

Last is the last element in List. Example:

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

max_list(+ListOfNumbers, ?Max)

Max is the largest of the elements in ListOfNumbers.

member(?Element, ?List)

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 ? <RET>
          
          yes
          

memberchk(+Element, +List)

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_list(+ListOfNumbers, ?Min)

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

nextto(?X, ?Y, ?List)

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
          

no_doubles(?List)

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

non_member(?Element, ?List)

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

nth(?N, ?List, ?Element)

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
          

nth(?N, ?List, ?Element, ?Rest)
Element is in position N in the List and Rest is all elements in List except Element.
nth0(?N, ?List, ?Element)

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

nth0(?N, ?List, ?Element, ?Rest)
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]
          

permutation(?List, ?Perm)

Perm is a permutation of List.

prefix(?Prefix, ?List)

Prefix is a prefix of List. Example:

          | ?- prefix([1,2,3], [1,2,3,4,5,6]).
          

remove_duplicates(+List, ?Pruned)

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
          

reverse(?List, ?Reversed)

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

same_length(?List1, ?List2)

List1 and List2 have the same number of elements.

same_length(?List1, ?List2, ?Length)
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
          

select(?Element, ?List, ?List2)

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

sublist(?Sub, ?List)

Sub contains some of the elements of List, in the same order.

substitute(+X, +Xlist, +Y, ?Ylist)

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]
          

sum_list(+ListOfNumbers, ?Sum)

Sum is the result of adding the ListOfNumbers together.

suffix(?Suffix, ?List)

Suffix is a suffix of List.