1
:-dynamic listofQuestions/2.
myrule:-
    write('P = '), write(Percent), write('-'),write(X),
    ( listofQuestions(Percent,X) -> true ; assert(listofQuestions(Percent,X)) ),

The code snippet might not be required to answer my question.

I want to assert to a blank 'listofQuestions' everytime I call my rule. This only happens if I close my prolog window and restart it.

Any suggestions?

false
  • 10,264
  • 13
  • 101
  • 209
HungryCoder
  • 1,029
  • 2
  • 10
  • 16
  • possible duplicate of [Prolog - How to assert/make a database only once](http://stackoverflow.com/questions/10437395/prolog-how-to-assert-make-a-database-only-once) – Tamara Wijsman May 03 '12 at 19:23
  • Maybe, because that question was asked by me as well. I need to wipe whatever it was stored everytime my program exits. It satisfies what I need, everytime PROLOG exits. But that isn't what I need. :( – HungryCoder May 03 '12 at 19:35

1 Answers1

4

abolish/1 removes all clauses of a given predicate from the database. Hence, just add a call to abolish(PredName/Arity) whenever you need to remove the information about this predicate. Beware that after abolishing the call to the dynamic predicate does not fail but reports an error.

12 ?- f(X,Y).
false.

13 ?- assert(f(a,b)).
true.

14 ?- f(X,Y).
X = a,
Y = b.

15 ?- abolish(f/2).
true.

16 ?- f(X,Y).
ERROR: user://2:67:
        toplevel: Undefined procedure: f/2 (DWIM could not correct goal)

In SWI-Prolog, abolish works on static procedures, unless the prolog flag iso is set to true. If you intend to remove only dynamic predicates, you should better try retractall. Observe that in this case removal does not lead to an error being reported but to a failure.

17 ?- [user].
:- dynamic f/2.
|: 
% user://3 compiled 0.00 sec, 264 bytes
true.

18 ?- f(X,Y).
false.

19 ?- assert(f(a,b)).
true.

20 ?- f(X,Y).
X = a,
Y = b.

21 ?- retractall(f(X,Y)).
true.

22 ?- f(X,Y).
false.
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32