My example is using the append functor:
append([],L,L).
append([H|T],L2,[H|L3]) :- append(T,L2,L3).
//append([123],[abc],L3). this is the query
What really confuses me is the [H|L3] list. Form what I read, that takes off the head, so how is that appending it to the list. When it recurses through the list, and starts coming back, how is that appended? Thanks in advance.
You can use trace/0
in those cases when you wonder what the execution looks like.
Here is an overview of it.
append([1, 2, 3], [a, b, c], L3).
[1, 2, 3]
is not empty, the second clause is appliedappend([2, 3], [a, b, c], L4).
and remembers that the head of L3
is like the head of [1, 2, 3]
and that the tail of L3
is L4
[2, 3]
is not empty, the second clause is appliedappend([3], [a, b, c], L5).
and remembers that the head of L4
is like the head of [2, 3]
and that the tail of L4
is L5
[3]
is not empty, the second clause is appliedappend([], [a, b, c], L6).
and remembers that the head of L5
is like the head of [3]
and that the tail of L5
is L6
[]
unifies with []
, the first clause is appliedL6
unifies with [a, b, c]
Now Prolog has all the informations to answer you:
L6 = [a, b, c],
L5 = [3|L6],
L4 = [2|L5],
L3 = [1|L4].
will allow Prolog to conclude L3 = [1, 2, 3, a, b, c]
.
Hope it helped.