# Module lists

`:- use_module(library(lists)).`

List manipulation predicates

#### length(?Xs, ?N).

Relates a list to its length (number of elements). It can be used to count the elements of a current list or to create a list full of free variables with N length.

```
?- length("abc", 3).
true.
?- length("abc", N).
N = 3.
?- length(Xs, 3).
Xs = [_A,_B,_C].
```

#### member(?X, ?Xs).

Succeeds when X unifies with an item of the list Xs, which can be at any position.

```
?- member(X, "hello world").
X = h
; ... .
```

#### select(X, Xs0, Xs1).

Succeeds when the list Xs1 is the list Xs0 without the item X

```
?- select(c, "abcd", X).
X = "abd"
; false.
```

#### append(+XsXs, ?Xs).

Concatenates a list of lists

```
?- append([[1, 2], [3]], Xs).
Xs = [1,2,3].
```

#### append(Xs0, Xs1, Xs).

List Xs is the concatenation of Xs0 and Xs1

```
?- append([1,2,3], [4,5,6], Xs).
Xs = [1,2,3,4,5,6].
```

#### memberchk(?X, +Xs).

This predicate is similar to `member/2`

, but it only provides a single answer

#### reverse(?Xs, ?Ys).

Xs is the Ys list in reverse order

?- reverse([1,2,3], [3,2,1]). true.

#### maplist(+Predicate, ?Xs0).

This is a metapredicate that applies predicate to each element of the list Xs0

```
?- maplist(write, [1,2,3]).
123 true.
```

#### maplist(+Predicate, ?Xs0, ?Xs1).

This is a metapredicate that applies predicate to each element of the lists Xs0 and Xs1.

```
?- maplist(length, ["hello", "prolog", "marseille"], Xs1).
Xs1 = [5,6,9].
```

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1 and Xs2.

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2, ?Xs3).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1, Xs2 and Xs3.

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2, ?Xs3, ?Xs4).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1, Xs2, Xs3 and Xs4.

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2, ?Xs3, ?Xs4, ?Xs5).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1, Xs2, Xs3, Xs4 and Xs5.

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2, ?Xs3, ?Xs4, ?Xs5, ?Xs6).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1, Xs2, Xs3, Xs4, Xs5 and Xs6.

#### maplist(+Predicate, ?Xs0, ?Xs1, ?Xs2, ?Xs3, ?Xs4, ?Xs5, ?Xs6, ?Xs7).

This is a metapredicate that applies predicate to each element of the lists Xs0, Xs1, Xs2, Xs3, Xs4, Xs5, Xs6 and Xs7.

#### sum_list(+Xs, -Sum).

Takes a lists of numbers and unifies Sum with the result of summing all the elements of the list.

```
?- sum_list([2,2,2], 6).
true.
```

#### same_length(?Xs, ?Ys).

Succeeds if Xs and Ys are lists of the same length

#### foldl(+Predicate, ?Ls, +A0, ?A).

foldl, sometimes called reduce, is a metapredicate that takes a predicate, a list of items and a starting value, and outputs a single value. The predicate *Predicate* must be able to take the current element of the list, the previous value of the computation and the next value of the computation.

For example, if we define sum_ as:

```
sum_(L, S0, S) :- S is S0 + L.
```

Then we can define `sum_list/2`

as the following:

```
sum_list(Ls, S) :- foldl(sum_, Ls, 0, S).
```

#### foldl(+Predicate, ?Ls0, ?Ls1, +A0, ?A).

Same as `foldl/4`

but with an extra list

#### transpose(?Ls, ?Ts).

If Ls is a list of lists, Ts contains the transposition

```
?- transpose([[1,1],[2,2]], Ts).
Ts = [[1,2],[1,2]].
```

#### list_to_set(+Ls0, -Set).

Takes a list Ls0 and returns a list Set that doesn't contain any repeated element

```
?- list_to_set([2,3,4,4,1,2], Set).
Set = [2,3,4,1].
```

#### nth0(?N, ?Ls, ?E).

Succeeds if in the N position of the list Ls, we found the element E. The elements start counting from zero.

```
?- nth0(2, [1,2,3,4], 3).
true.
```

#### nth1(?N, ?Ls, ?E).

Succeeds if in the N position of the list Ls, we found the element E. The elements start counting from one.

```
?- nth1(2, [1,2,3,4], 2).
true.
```

#### nth0(?N, ?Ls, ?E, ?Rs).

Succeeds if in the N position of the list Ls, we found the element E and the rest of the list is Rs. The elements start counting from zero.

```
?- nth0(2, [1,2,3,4], 3, [1,2,4]).
true.
```

#### nth1(?N, ?Ls, ?E, ?Rs).

Succeeds if in the N position of the list Ls, we found the element E and the rest of the list is Rs. The elements start counting from one.

```
?- nth1(2, [1,2,3,4], 2, [1,3,4]).
true.
```

#### list_max(+Xs, -Max).

Takes a list Xs and unifies with the maximum value of the list

#### list_min(+Xs, -Min).

Takes a list Xs and unifies with the minimum value of the list

#### permutation(?Xs, ?Ys) is nondet.

True when Xs is a permutation of Ys. This can solve for Ys given Xs or Xs given Ys, or even enumerate Xs and Ys together. The predicate `permutation/2`

is primarily intended to generate permutations. Note that a list of length N has N! permutations, and unbounded permutation generation becomes prohibitively expensive, even for rather short lists (10! = 3,628,800).

The example below illustrates that Xs and Ys being proper lists is not a sufficient condition to use the above replacement.

```
?- permutation([1,2], [X,Y]).
X = 1, Y = 2
; X = 2, Y = 1
; false.
```

Throws `type_error(list, Arg)`

if either argument is not a proper or partial list.