我正在做一个 Prolog 项目,下面是我的代码。
:- dynamic yes/1,no/1.
:- dynamic n_angle/1.
go :- hypothesize(Shape),
write('Yes I know the shape you talking about is a '),
write(Shape),
write('!!!!'),
undo.
hypothesize(circle) :- circle,!.
circle :- not(verify(width)),
verify(radius),
not(verify(height)),
verify(diameter_equal_2_radius).
ask(Question):-
write('Has the shape '),
write(Question),
write('?'),
read(Response),
nl,
((Response == yes ; Response == y)
-> assert(yes(Question));
assert(no(Question)), fail).
verify(S) :-
(yes(S) -> true ;
(no(S) -> fail ;
ask(S))).
save_file:- tell('D:ansSave.txt').
/* undo all yes/no assertions */
undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo :- retract(n_angle(_)),fail.
undo.
结果会是这样。
?- 去。具有形状宽度?n。
有形状半径吗?y。
具有形状高度?n。
形状为 diameter_equal_2_radius?y。
是的,我知道你说的形状是圆形!!!!
真的。
我想将如上所示的结果保存到 txt 文件中。
但是当我尝试将 save_file 放入 ask 函数时
ask(Question):-
save_file,
write('Has the shape '),
write(Question),
write('?'),
read(Response),
nl,
told,
((Response == yes ; Response == y)
-> assert(yes(Question));
assert(no(Question)), fail).
每次都会覆盖结果。谁能告诉我如何解决这个问题?提前致谢。