projog

3.110. length(X,Y) - determines the length of a list.

The length(X,Y) goal succeeds if the number of elements in the list X matches the integer value Y.

Examples

?- length([],X).
X = 0

yes

?- length([a],X).
X = 1

yes

?- length([a,b],X).
X = 2

yes

?- length([a,b,c],X).
X = 3

yes

?- length([a,b],1).

no

?- length([a,b],3).

no

?- length(X,0).
X = []

yes

?- length(X,1).
X = [E0]

yes

?- length(X,3).
X = [E0,E1,E2]

yes

?- length(X,Y).
X = []
Y = 0

yes;
X = [E0]
Y = 1

yes;
X = [E0,E1]
Y = 2

yes;
X = [E0,E1,E2]
Y = 3

yes;
X = [E0,E1,E2,E3]
Y = 4

yes;
X = [E0,E1,E2,E3,E4]
Y = 5

yes;
X = [E0,E1,E2,E3,E4,E5]
Y = 6

yes;
X = [E0,E1,E2,E3,E4,E5,E6]
Y = 7

yes

?- length([a,b|X],Y).
X = []
Y = 2

yes;
X = [E0]
Y = 3

yes;
X = [E0,E1]
Y = 4

yes;
X = [E0,E1,E2]
Y = 5

yes;
X = [E0,E1,E2,E3]
Y = 6

yes;
X = [E0,E1,E2,E3,E4]
Y = 7

yes;
X = [E0,E1,E2,E3,E4,E5]
Y = 8

yes;
X = [E0,E1,E2,E3,E4,E5,E6]
Y = 9

yes

TODO fix documentation generator to handle QUIT

?- length([a,b|X],8).
X = [E0,E1,E2,E3,E4,E5]

yes

?- length([a,b|X],3).
X = [E0]

yes

?- length([a,b|X],2).
X = []

yes

?- length([a,b|X],1).

no

?- length([a,b,c],a).

no

?- length(X,X).

no

?- length([a,b,c|X],X).

no

?- length(abc,X).

Expected list but got: ATOM with value: abc

?- length([a,b|c],X).

Expected list but got: LIST with value: .(a, .(b, c))

?- length([a,b|X],z).

Expected Numeric but got: ATOM with value: z