projog

3.109. last(X,Y) - finds the last element of a list.

Examples

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

yes

?- last([q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m], X).
X = m

yes

?- last([a], X).
X = a

yes

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

no

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

no

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

yes

?- last([a,b,c], d).

no

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

no

?- last([a,b|[]], b).

yes

?- last([], X).

no

?- last(a, X).

no

?- last(Y, X).
X = UNINSTANTIATED VARIABLE
Y = [X]

yes;
X = UNINSTANTIATED VARIABLE
Y = [_,X]

yes;
X = UNINSTANTIATED VARIABLE
Y = [_,_,X]

yes;
X = UNINSTANTIATED VARIABLE
Y = [_,_,_,X]

yes

?- Z=[a,b|Tail],last(Z,Last).
Last = b
Tail = []
Z = [a,b]

yes;
Last = UNINSTANTIATED VARIABLE
Tail = [Last]
Z = [a,b,Last]

yes;
Last = UNINSTANTIATED VARIABLE
Tail = [_,Last]
Z = [a,b,_,Last]

yes;
Last = UNINSTANTIATED VARIABLE
Tail = [_,_,Last]
Z = [a,b,_,_,Last]

yes

?- Z=[a,b|Tail],last(Z,a).
Tail = [a]
Z = [a,b,a]

yes;
Tail = [_,a]
Z = [a,b,_,a]

yes;
Tail = [_,_,a]
Z = [a,b,_,_,a]

yes;
Tail = [_,_,_,a]
Z = [a,b,_,_,_,a]

yes

?- Z=[a,b|Tail],last(Z,b).
Tail = []
Z = [a,b]

yes;
Tail = [b]
Z = [a,b,b]

yes;
Tail = [_,b]
Z = [a,b,_,b]

yes;
Tail = [_,_,b]
Z = [a,b,_,_,b]

yes

?- Z=[a,b|Tail],last(Z,z).
Tail = [z]
Z = [a,b,z]

yes;
Tail = [_,z]
Z = [a,b,_,z]

yes;
Tail = [_,_,z]
Z = [a,b,_,_,z]

yes;
Tail = [_,_,_,z]
Z = [a,b,_,_,_,z]

yes