Before this program I've seen in the following example that say if an element X belongs to a String L.
The code of this example is the following:
member2(X, [X|_]).
member2(X,[_|T]):- member2(X,T).
I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list) and the rule is used to recursively search in the tail of the list if the element X is not the head...
This version was pretty obscure for me and someone advised me to see it in this way to understand how it work:
member2(X, [X|_]).
member2(X,Y):- Y = [_|T],
member2(X,T).
So the meaning is more clear:
I have one fact that represent the base case (the X element belongs to the list if it is in the head of the list).
The rule say that I have to prove two things:
1) Y = [_|T] This is true because the anonymous variable _ unifies with anything
2) It recursively search the element X in the tail list
Ok, now I have a problem to understand this new exercise, I have to write a SWI Prolog program that, given two strings, concatenate the second string to the first in a third string
The solution is:
myappend([], L, L).
myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).
I have some problem to understand how it work
I have understand that the fact represents the base case: when the first parameter is a void list [] and the second parameter is a not void list, the concatenation of the first parameter and second parameter is the second parameter list (and this equals to the third parameter)
The rule represents the general case in which the first parameter is not a void list...so I think that when this case occurs it delete the head of L1 parameter (the head of the first list) until this is an empty list [], so I will have the base case and the third parameter is unified to the second one...then do backtracking...
But I have problem to understand how exactly work the rule:
myappend([X|L1], L2, [X|L]) :- myappend(L1,L2,L).
I am trying to do something like the previous example and translate it in a more speaking logic form because in this way is very unclear for me....but I have not ideas how to do...how can I do
What is the logic behind this rule? what does it mean?