i am doing a Prolog project and below is my code.
:- 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.
and the result will be like this.
?- go.
Has the shape width?n.
Has the shape radius?y.
Has the shape height?n.
Has the shape diameter_equal_2_radius?y.
Yes I know the shape you talking about is a circle!!!!
true.
i want to save the result as shown above to a txt file.
but when i try to put the save_file to the ask function
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).
it will overwrite the result every time. can anyone tell me how to solve this ? thanks in advance.
If you don’t want to overwrite previous file content, consider using
append/1.So your rule must be: