1
votes
member(X,[X|T]).
member(X,[H|T]):-member(X,T).

delfstocc(X,[X|T],T).
delfstocc(X,[Y|T],[Y|T1]):-delfstocc(X,T,T1),!.

delallocc(X,L,L1):-member(X,L),delfstocc(X,L,R),!,delallocc(X,R,L1);write(L).

ـــــــــــ

first , i wrote this code without using the cut , then i tried to put the cut operator after each predicate untill i get the perfect answer but actually i don't understand how it works after using the cut . i know that the cut operator stops prolog from proceeding matching , however i can't use it correctly , so i want help tracing this code by the way , this code simly delete all occurrences of an element in a list .

1
Can you try and format your question? It will be easier to read...user1812457
am sorry i am new on the site , actually that's my first questionjustnedved

1 Answers

1
votes

First of all, as far as cuts are concerned, it is actually handled as follows:

"The goal succeeds and commits Prolog to all the choices made since the parent goal was unified with the head of the clause the cut occurs in".

See also: http://www.swi-prolog.org/pldoc/doc_for?object=!/0

About deleting from a list: to achieve what you want, your approach is unnecessarily complicated. Here is a more simple solution:

For the empty list, delete always holds true:

del([],_X,[]).

When the head of the list is the same as the element you are deleting, it does not belong to the other list:

del([X|Xs], X, Ys) :- del(Xs, X, Ys).

When the head of the list is not the same as the element you are deleting, it belongs to the other list:

del([X|Xs], Z, [X|Ys]) :- dif(X,Z), del(Xs, X, Ys).

(Note: the order of the clauses is significant! Why?)

This solution does not use cuts and attempts backtracking. You could use cuts at the appropriate places to prevent backtracking.