The whole question is define the following predicates in prolog:
1, lastelem(L,X) which is true only when X is the last element in the list L.
2, replast(L,X,M) which is true only when M is the same as L except(possibly) for the last element which is X for M.
3, exchfal(L,M) which is true only when M is the same as L except that the first element of L is the last element of M and vice versa. If either L or M is empty or has length 1, then they should be equal.
Here is a samle session:
| ?-lastelem([1,2,3,4],X).
X = 4 ?;
no
| ?-replast([1,2,3,4],5,Z)
Z =[1,2,3,5] ? ;
no
| ?-exchfal([1,2,3,4],U)
U =[4,2,3,1] ?;
no
| ?-exchfal([1,2,3,4,10],U)
U =[10,2,3,4,1] ? ;
no
1, lastelem(L,X) which is true only when X is the last element in the list L.
2, replast(L,X,M) which is true only when M is the same as L except(possibly) for the last element which is X for M.
3, exchfal(L,M) which is true only when M is the same as L except that the first element of L is the last element of M and vice versa. If either L or M is empty or has length 1, then they should be equal.
Here is a samle session:
| ?-lastelem([1,2,3,4],X).
X = 4 ?;
no
| ?-replast([1,2,3,4],5,Z)
Z =[1,2,3,5] ? ;
no
| ?-exchfal([1,2,3,4],U)
U =[4,2,3,1] ?;
no
| ?-exchfal([1,2,3,4,10],U)
U =[10,2,3,4,1] ? ;
no