0
votes

I want to delete the first fact that fulfills my condition in Prolog.

I tried to delete one and only one of the five facts that its number not equal to my goal.

My goal here is to keep the cards with number 4.

The cuts operation doesn't work with negation.

In a clear sentence I want to delete this fact (has(reem,blue,1)) which is the first fact that fulfills my condition.

How can I solve this problem?

:- dynamic
        has/3, first/2.

has(reem,yellow,4).
has(reem,blue,1).
has(reem,red,5).
has(reem,green,4).
has(reem,blue,2).


deleteCard(Player,Goal):-
    retract(has(Player,_,Y)),not(Y=Goal),!.

start:-
   deleteCard(reem,4),
    displayAll(reem).

displayAll(Player):-
	nl,
	write('**LIST OF ALL CARDS YOU HAVE**'),
	nl,
	forall(has(Player,X,Y),(writeln(X+Y))).
1
I find it difficult to understand what you are doing, what you want to do, and why there is a problem.user1812457
what I want is to find a way to get the first fact using cuts with condition (negation), then If I got this fact I will delete it.Reem Aljunaid
In a clear sentence I want to delete this fact (has(reem,blue,1)). by using cuts and negationReem Aljunaid
this will delete the first fact with number 4, I want to delete the first fact with number not equal to 4.Reem Aljunaid
But if you want to retract one of the clauses of has/3, why are you writing retract(first(...))? Shouldn't it be retract(has(...))?user1812457

1 Answers

1
votes

This is the solution :

:- dynamic
        has/3.

has(reem,blue,2).
has(reem,blue,1).
has(reem,red,5).
has(reem,yellow,4).
has(reem,green,4).


deleteCard(Player,Goal):-
    has(Player,_,Y),not(Y=Goal),!,retract(has(Player,_,Y)).

start:-
   deleteCard(reem,4),
    displayAll(reem).

displayAll(Player):-
	nl,
	write('**LIST OF ALL CARDS YOU HAVE**'),
	nl,
	forall(has(Player,X,Y),(writeln(X+Y))).