Question

I'm planning to make new facts based on existing facts, by using assert. However, the number of facts to be made will be more than 500, so that typing semicolon to go further steps become pretty tedious work. thus I want to ignore or pass the 'true'(in the SWI PROLOG)

Are there any ways to deal with this?(ex. automatically pass all the 'true's...)

here's a part of my code

%initialize
initialize :-
   discipline(X,Y),
   assert(result(X,0)).

I have too many Xs in discipline(X,Y)..

Was it helpful?

Solution

maybe

?- forall(a_fact(F), your_fact_processing(F)).

OTHER TIPS

In this specific case forall is actually preferred, but in general, in Prolog you have to rely on the language's mechanism for this kind of iteration. Here's an example for your case:

initialize:- 
   discipline(X,Y),
   assert(result(X,0)),
   fail.
initialize.

In this bit of code above, you are telling the interpreter that initialize should perform all the 'asserts' given possible disciplines through the backtracking mechanism. Unless you become really familiar with this, Prolog will never "click" for you.

Note that in this example initialize will never fail, even if there are no disciplines (and therefore no results) to assert. You'll need some extra work to detect edge-cases like that - which is why forall is actually preferred for this specific task of assertin many facts.

Also note that if it's good practice to not have singleton variables declared, you can use the notation where variables that you won't use start with the '_' (underscore) character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top