Delete the first fact that fulfills a condition in Prolog

657 views Asked by At

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

There are 1 answers

0
Reem Aljunaid On

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))).