Module lists

This module provides persistent lists. Prolog Lists are written as [x1,..,xn] and are internally constructed by the pairing constructor [h|t] and the empty constructor []. The length of such a list is n and the i-th element is xi. Most predicates are implemented such that they leave as few as possible choice points.

Example:
?- last([1,2,3], X).
X = 3
?- last([1,2,3], X, Y).
X = 3,
Y = [1,2]

The predicates append/3, reverse/2, member/2, select/3, last/2 and last/3 work directly with lists. The predicates length/2, nth0/3, nth0/4, nth1/3 and nth1/4 take also a length respective index into account. The predicates maplist/n and foldl/n have a closure argument to apply a predicate repeatedly to a number of arguments.

The following list predicates are provided:

append(L1, L2, L3):
The predicate succeeds whenever L3 unifies with the concatenation of L1 and L2.
reverse(L1, L2):
The predicate succeeds whenever L2 unifies with the reverse of L1. The current implementation does not terminate on redo for an input L2 and an output L1.
member(E, L):
The predicate succeeds for every member E of the list L.
select(E, L, R):
The predicate succeeds for every member E of the L with remainder list R.
last(L, E):
The predicate succeeds with E being the last element of the list L.
last(L, E, R):
The predicate succeeds with E being the last element of the list L and R being the remainder of the list.
length(L, N):
The predicate succeeds with N being the length of the list L.
nth0(I, L, E):
The predicate succeeds with E being the (I+1)-th element of the list L.
nth0(I, L, E, R):
The predicate succeeds with E being the (I+1)-th element of the list L and R being the remainder of the list.
nth1(I, L, E):
The predicate succeeds with E being the I-th element of the list L.
nth1(I, L, E, R):
The predicate succeeds with E being the I-th element of the list L and R being the remainder of the list.
maplist(C, L1, ..., Ln):
The predicate succeeds in applying the closure C to the elements of L1, ..., Ln. The predicate is currently defined for 1 ≤ n ≤ 4.
foldl(C, L1, ..., Ln, I, O):
The predicate succeeds in applying the closure C to the elements of L1, ..., Ln and accumulating the result among I and O. The predicate is currently defined for 1 ≤ n ≤ 4.

Comments