Lately I have started to study Prolog , as soon as I've dealt with it I had issues to understand it.
I'm studying the list and I can't understand how the append/3
predicate, which should merge two list, works.
book_append([], L, L).
book_append([X|L1], L2, [X|L3]) :-
book_append(L1, L2, L3).
Precisely I can't understand how L2
is appended to main list. I try to explain what I have understood so far:
The boundary condition will unify only when the first list is empty and the the second and third will take the same value. is it correct?
First question, maybe it is stupid, when the first list is empty the two list must unify so they will take the same value. But they should be different so I will lose one of their value.
In the second line the head of the first list is appended on the third list and recall the same function.
I usually use this function in this way: book_append([1, 2, 3], [a, b, c], X).
The first line is false because L1
isn't empty.
While the second one should be :
book_append([1|[2, 3], [a, b, c], [1|L3]) :-
book_append([2, 3], [a, b, c], L3).
What's the value of L3
(I already know it's the tail of the third list) during the first call? Is it empty? Or an uninstantiated variable? Or is it instantiated to L2
?