当然..使用序言数据库,断言和撤回。这证明了这一点:
% Declare the lasthead fact as dynamic, so facts can change
:-dynamic lasthead/1.
% Set a starting value for the first iteration
lasthead(null).
statement([]).
statement([A|B]) :-
% Show the previous head
lasthead(LH),
writeln(['Last head was', LH]),
% Retract last head. ie. remove from the database
retract(lasthead(_)),
% Store the current head in the database
assertz(lasthead(A)),
% Recurse around
statement(B).
?- statement([a,b,c,d,e]).
[Last head was,null]
[Last head was,a]
[Last head was,b]
[Last head was,c]
[Last head was,d]
上面的示例使用retract 来确保只有一次lasthead(X) 事实,但您可以删除retract,这将确保有多个lasthead(X) 事实,每个列表项一个。
然后,您可以使用例如访问/处理多个 lasthead(X) 事实。findall(X, lasthead(X), Y),它将为您提供您在此过程中断言的任何 lasthead(X) 值。