This is an algorithm to append together two lists:
Domains
list= integer*
Predicates
nondeterm append(list, list, list)
Clauses
append([], List, List) :- !.
append([H|L1], List2, [H|L3]) :- append(L1, List2, L3).
Goal
append([9,2,3,4], [-10,-5,6,7,8], Ot).
The result is a list [9,2,3,4,-10,-5,6,7,8]
, and it's saved in "Ot
".
My question is, how does this work?
What I understand is that in every recursive call, in the first list, you get only the tail as a list ( thus reducing its size by 1
until it's []
), the second argument "List2
" does not change at all, and the 3rd argument ... at first it's []
, and after each recursive call you get its tail, but since it's []
, it stays []
.
So how come, suddenly, in 3rd argument ("Ot
") we have the appended list ?
Can someone explain this algorithm step by step ?
First, let's translate the clauses into something more understandable:
can be written
and
can be written
I hope this will already be clearer for you.
Then, step by step, the number indicates the clause used each time, and the resulting call is shown:
At this step all the values we're interested in are already defined. Notice how the head of the result is set before its tail is filled up by a subsequent (tail recursive) call to
append
, building the resulting list in the characteristic for Prolog top-down fashion (also known as "tail recursion modulo cons").Let's follow the definitions to see what
Ot
is, at the final step:I hope you'll get something out of it.