These predicates enable lists to be manipulated and trasversed.
length/2
length(List, Length)
is true iff integer
Length
equals the number of elements in list
List
. In particular, if
List
is instantiated to a list of determinate length, then
Length
will be unified with this length. If
List
is of indeterminate length and
Length
is instantiated to an integer, then
List
will be unified with a list of length
Length
, and the list elements are unique variables. Finally, if
Length
is unbound then
Length
will be unified with all possible lengths of
List
.
Templates and modes for the predicate are as follows:
length(?list, ?length)
Let's start with some simple tests verifying success or failure of single goals.
Goal | Theory | success(String goal,String theory) |
---|---|---|
length([], 0). | null | true |
length(X, 5). | null | true |
length([1, 2 | T], X). | null | true |
length(L, S). | null | true |
Goal | Theory | success(String goal,String theory) |
---|---|---|
length('scarlet', 7). | null | false |
length(A, -1). | null | false |
Tests With Results
Goal | Theory | Variable | Solution | Limit | success(String goal,String theory,String variable,Strng solution) |
---|---|---|---|---|---|
length(X, 5). | null | X | [_,_,_,_,_] | 10 | True |
length([1, 2 | T], X). | null | X | 2 | 10 | True |
length([1, 2 | T], X). | null | T | [] | 10 | True |
length([1, 2 | T], X). | null | X | 3 | 10 | True |
length([1, 2 | T], X). | null | T | [_] | 10 | True |
length([1, 2 | T], X). | null | X | 4 | 10 | True |
length([1, 2 | T], X). | null | T | [_,_] | 10 | True |
length(L, S). | null | L | [] | 10 | True |
length(L, S). | null | S | 0 | 10 | True |
length(L, S). | null | L | [_] | 10 | True |
length(L, S). | null | S | 1 | 10 | True |
length(L, S). | null | L | [_,_] | 10 | True |
length(L, S). | null | S | 2 | 10 | True |
Other candidates for similar tests are:
append/3
,
member/2
,
no_duplicates/2
,
quicksort/3
,
reverse/2
,
delete/3
,
element/3
.