You have described some cases where del/3
should hold. But these are only cases where Ele
is not equal/unifiable to the elements in the list. There are several things missing:
What about the empty list?
What, if Ele
is equal to the element?
So you need to add further clauses. (Later you might also remove some, for reasons of redundancy).
If you are using SWI, B, SICStus, or YAP, consider to use dif/2
in place of (\=)/2
. prolog-dif
Here is the reason why dif/2
is so helpful in this case. With dif/2
you would have a pure monotonic program. And you could try it out directly:
?- del(5, [3,4,5,6,7], X). false.
The same problem you had. Let me just restate what the problem is: You expect that something should hold, but it does not. So the relation is too narrowly defined. If I generalize the query, I might get a better answer. Try del(E, [3,4,5,6,7], X).
but again the same false
. So I'll try an even more general query:
?- del(E, Xs, Ys). Xs = Ys, Ys = [_G1076], dif(E, _G1076) ...
Looks perfect to me! Maybe another answer:
?- del(E, Xs, Ys). Xs = Ys, Ys = [_G1076], dif(E, _G1076) ; Xs = [_G1133, _G1136], Ys = [_G1133|_G1136], dif(E, _G1136), dif(E, _G1133) ...
Now we got an incorrect answer. I will instantiate it a bit to make it better readable:
?- del(e, [a,b], Ys). Ys = [a|b] ; false.
This answer is clearly incorrect because [a|b]
is not a list. And, it's probably the smallest incorrect answer...
What I want to show you by this is that you can most often locate problems without going through it step-by-step. Step-by-step debugging does not even work in imperative languages once you get a more complex control flow (like concurrency) ; and it does not scale at all in Prolog.