2

(1) how to define the delete(X,L1,L2) relation using Prolog where L2 is the resulted list in which item X is deleted from list L1

(2) How to draw the derivation tree to answer the following query?

| ?- delete(3,[4,10,6,8,1,3,9],L)

my attempt

thsumia
  • 53
  • 1
  • 9
  • You should show your attempt at a solution to this problem. It also needs clarification: does `delete(X, L1, L2)` delete *one* instance of `X` from `L1`, or *all* instances? – lurker Jun 12 '15 at 10:40
  • I'm not sure I understand question #2. You asked for a derivation tree, and then you showed one. – lurker Jun 12 '15 at 18:39

1 Answers1

3

Save by using the tfilter/3 and reified disequality dif/3:

?- tfilter(dif(3),[      4,    10,6,8,1,3,9],[4,10,6,8,1,9]).
true.
?- tfilter(dif(3),[3,3,3,4,3,3,10,6,8,1,3,9],[4,10,6,8,1,9]).
true.

Note this works just as fine with non-ground terms, too!

?- tfilter(dif(Y),[A,B],Xs).
Xs = [   ], Y = A,    A = B    ;
Xs = [  B], Y = A,    dif(A,B) ;
Xs = [A  ], Y = B,    dif(B,A) ;
Xs = [A,B], dif(Y,B), dif(Y,A).
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166
  • what u mean by false..it should be true..isn't it??..When i compile it in SWI it gets the answer as true. and when i run the program it gets the answer as [4,10,6,8,1,9] . So what u mean as False?? – thsumia May 11 '15 at 01:46
  • 1
    @thsumia. I was referring to the user named "false". I hope that did not cause too much confusion. – repeat Jun 12 '15 at 18:34