使用tell函数在Prolog中保存答案

时间:2012-08-26 05:16:50

标签: prolog

我正在做一个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。

形状高度是多少?

形状为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).

每次都会覆盖结果。任何人都可以告诉我如何解决这个问题?提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您不想覆盖以前的文件内容,请考虑使用append/1

所以你的规则必须是:

save_file:- append('D:ansSave.txt').