I have a doubt about how work this query for this del/3 predicate:
/* BASE CASE: If I delete X from List and X is the HEAD of List, NewList is
the Tail of List
*/
del(X, [X|Tail], Tail).
/* GENERAL CASE: If the head of List is not X then the program have to delete
X in the Tail of List
*/
del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1).
The predicate logic is very simple: delete X item form a list creating a new list without X: if X is in the head of the list the newlist is its tail. Otherwise, if the X item is not in the head of the list, try to find it (and delete) in the Tail creating a new tail Tail1.
Ok, so I have no problem with the predicate logic but I am having some problem trying to understand how work this query (I have to use it in another program):
del([Top1|Stack1], [[a,b,c],[],[]], Stacks1).
So this query have to delete [Top1|Stack1] from [[a,b,c],[],[]] that is a list of stacks (in this particular case I have 3 stacks: [a,b,c] and 2 empty stacks: []) generating so a new list of stacks named Stacks1
If I try to execute a trace of the query I obtain this:
[trace] ?- del([Top1|Stack1], [[a,b,c],[],[]], Stacks1).
Call: (7) del([_G389|_G390], [[a, b, c], [], []], _G412) ? creep
Exit: (7) del([a, b, c], [[a, b, c], [], []], [[], []]) ? creep
Top1 = a,
Stack1 = [b, c],
Stacks1 = [[], []] .
I have some difficulties to understand why: [Top1|Stack1] is unified with the first stack [a, b, c]
EDIT: I think that maybe work in this way: The list of Stacks is: [[a,b,c],[],[]] that is a list of lists wherein the first list is: [a,b,c] (that is the head of this list of list**
So when I write: [Top1|Stack1] happens that:
Top1 = [a,b,c] *Stack1 = [[],[]]*
So happens that Top1 is the first stack in the stacks list and Stack1 is the list of others stacks.
So when I write the predicate:
del([Top1|Stack1], Stacks, Stacks1).
(where, for example: Stacks = [[a,b,c],[],[]])
It work in this way:
It unifiest Top1 with the first stack in stack list: [a,b,c] and delete it from Stacks list...
My dount is related on Prolog semantic viz when I execute a simple query as:
del(b, [a,b,c], NewList).
it delete b item from the list and NewList=[a,c]
but when I have that the field of the item that have be delete is something like: [Head|Tail] is it the Head item that have be to deleted?