projog

3.114. nth0(X,Y,Z) / nth1(X,Y,Z) - examines an element of a list.

Indexing starts at 0 when using nth0. Indexing starts at 1 when using nth1.

Examples

?- nth0(0, [a,b,c], a).

yes

?- nth1(1, [a,b,c], a).

yes

?- nth0(1, [a,b,c], b).

yes

?- nth1(2, [a,b,c], b).

yes

?- nth0(2, [a,b,c], c).

yes

?- nth1(3, [a,b,c], c).

yes

?- nth0(-1, [a,b,c], a).

no

?- nth0(1, [a,b,c], a).

no

?- nth0(5, [a,b,c], a).

no

?- nth0(0, [a,b,c], X).
X = a

yes

?- nth0(1, [a,b,c], X).
X = b

yes

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

yes

?- nth0(-1, [a,b,c], X).

no

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

no

?- nth0(X, [h,e,l,l,o], e).
X = 1

yes;

no

?- nth0(X, [h,e,l,l,o], l).
X = 2

yes;
X = 3

yes;

no

?- nth0(X, [h,e,l,l,o], z).

no

?- nth0(X, [h,e,l,l,o], Y).
X = 0
Y = h

yes;
X = 1
Y = e

yes;
X = 2
Y = l

yes;
X = 3
Y = l

yes;
X = 4
Y = o

yes

?- nth1(0, [a,b,c], a).

no

?- nth1(2, [a,b,c], a).

no

?- nth1(4, [a,b,c], a).

no

?- nth1(1, [a,b,c], X).
X = a

yes

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

yes

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

yes

?- nth1(-1, [a,b,c], X).

no

?- nth1(0, [a,b,c], X).

no

?- nth1(4, [a,b,c], X).

no

?- nth1(X, [h,e,l,l,o], e).
X = 2

yes;

no

?- nth1(X, [h,e,l,l,o], l).
X = 3

yes;
X = 4

yes;

no

?- nth1(X, [h,e,l,l,o], z).

no

?- nth1(X, [h,e,l,l,o], Y).
X = 1
Y = h

yes;
X = 2
Y = e

yes;
X = 3
Y = l

yes;
X = 4
Y = l

yes;
X = 5
Y = o

yes

Note: "nth" is a synonym for "nth1".

?- nth(2, [a,b,c], b).

yes

?- nth0(1, [h,e,l,l,o|Y], l).

no

?- nth1(1, [h,e,l,l,o|Y], l).

no

?- nth0(X, [h,e,l,l,o|Y], l).
X = 2
Y = UNINSTANTIATED VARIABLE

yes;
X = 3
Y = UNINSTANTIATED VARIABLE

yes;
X = 5
Y = [l|_5]

yes;
X = 6
Y = [_6,l|_5]

yes;
X = 7
Y = [_7,_6,l|_5]

yes

?- nth1(X, [h,e,l,l,o|Y], l).
X = 3
Y = UNINSTANTIATED VARIABLE

yes;
X = 4
Y = UNINSTANTIATED VARIABLE

yes;
X = 6
Y = [l|_6]

yes;
X = 7
Y = [_7,l|_6]

yes;
X = 8
Y = [_8,_7,l|_6]

yes

?- nth0(8,[a,b,c|X],Y).
X = [E4,E3,E2,E1,E0,Y|T]
Y = UNINSTANTIATED VARIABLE

yes

?- nth1(8,[a,b,c|X],Y).
X = [E3,E2,E1,E0,Y|T]
Y = UNINSTANTIATED VARIABLE

yes

?- nth0(X,[a,b,c|Y],Z).
X = 0
Y = UNINSTANTIATED VARIABLE
Z = a

yes;
X = 1
Y = UNINSTANTIATED VARIABLE
Z = b

yes;
X = 2
Y = UNINSTANTIATED VARIABLE
Z = c

yes;
X = 3
Y = [Z|_3]
Z = UNINSTANTIATED VARIABLE

yes;
X = 4
Y = [_4,Z|_3]
Z = UNINSTANTIATED VARIABLE

yes;
X = 5
Y = [_5,_4,Z|_3]
Z = UNINSTANTIATED VARIABLE

yes

?- nth1(X,[a,b,c|Y],Z).
X = 1
Y = UNINSTANTIATED VARIABLE
Z = a

yes;
X = 2
Y = UNINSTANTIATED VARIABLE
Z = b

yes;
X = 3
Y = UNINSTANTIATED VARIABLE
Z = c

yes;
X = 4
Y = [Z|_4]
Z = UNINSTANTIATED VARIABLE

yes;
X = 5
Y = [_5,Z|_4]
Z = UNINSTANTIATED VARIABLE

yes;
X = 6
Y = [_6,_5,Z|_4]
Z = UNINSTANTIATED VARIABLE

yes

?- nth0(X,Y,Z).
X = 0
Y = [Z|_0]
Z = UNINSTANTIATED VARIABLE

yes;
X = 1
Y = [_1,Z|_0]
Z = UNINSTANTIATED VARIABLE

yes;
X = 2
Y = [_2,_1,Z|_0]
Z = UNINSTANTIATED VARIABLE

yes

?- nth1(X,Y,Z).
X = 1
Y = [Z|_1]
Z = UNINSTANTIATED VARIABLE

yes;
X = 2
Y = [_2,Z|_1]
Z = UNINSTANTIATED VARIABLE

yes;
X = 3
Y = [_3,_2,Z|_1]
Z = UNINSTANTIATED VARIABLE

yes