这就是我只删除列表的最后一个元素的地方。
delete([_], []).
delete([X|Xs], [X|Last]) :- delete(Xs, Last).
循环应该一直运行到原始列表为空并返回新列表。
它有点类似于子集。
may be :
delete_last(In, Out) :-
append(Out, [_], In).
您可以改进您的定义,避免不必要的选择点,将其重写为:
delete([Head| Tail], List) :-
delete(Tail, Head, List).
delete([], _, []).
delete([Next| Tail], Previous, [Previous| List]) :-
delete(Tail, Next, List).
一些示例调用:
?- delete([1,2,3], L).
L = [1, 2].
?- delete([], L).
false.
?- delete([1], L).
L = [].
?- delete(L, [1,2,3]).
L = [1, 2, 3, _].
?- delete(L1, L2).
L1 = [_A],
L2 = [] ? ;
L1 = [_A,_B],
L2 = [_A] ? ;
L1 = [_A,_B,_C],
L2 = [_A,_B] ? ;
L1 = [_A,_B,_C,_D],
L2 = [_A,_B,_C] ?
...