Another problem, I have a good working “function” for deleting:
remove([],X,[]) :- !.
remove([X|T],X,L1) :- !, remove(T,X,L1).
remove([H|T],X,[H|L1]) :- remove(T,X,L1).
But it doesn’t work like I want it to work.
It removes element or even list…
…but doesn’t remove all appearances. This is goal:
remove([A,B,[C],[A,[B]],[[A,[B]]]],[A,[B]],X).
X=[A,B,[C],[]]
Any ideas?
If you would like to remove all appearances e.g., to let remove([1,2,3,1],1,X) calculate X=[2,3] just replace the second clause with
However, it seems that you would like a) to work with variables that should be kept as variables and b) to remove lists from sublists.
We will start with solving the b) issue first.
As you see we add a second recursive call to the last clause to take care of the internal list. Moreover, we had to add a special case (the second clause) since the first argument is not necessarily a list.
Finally, to solve a) you need to use the “freeze” / “melt” predicates of Sterling and Shapiro. freeze replaces variables with expressions #VAR(0), #VAR(1), … and melt does the opposite. Melt is traditionally known as melt_new
By the way, I really recommend the book of Sterling and Shapiro “The Art of Prolog”.
Melting and freezing are discussed in Chapter 15.