I'm having some troubles in understanding the following prolog predicate , I can understand that it concatenate some chars , also produces the possible lists (first & second goal ) , but I can't understand how it do this ? how it executes ?
domains
i=integer
l=i*
slist=string*
clist=char*
predicates
nondeterm conc(clist,clist,clist).
clauses
conc([],L,L).
conc([H|L1],L2,[H|L3]):-
conc(L1,L2,L3).
The first goal
goal
conc(['a','b'],['c','d'],L).
result
L=['a','b','c','d']
the second goal
goal
conc(L1,L2,['a','b','c']).
result
L1=[], L2=['a','b','c']
L1=['a'], L2=['b','c']
L1=['a','b'], L2=['c']
L1=['a','b','c'], L2=[]
4 Solutions
I'm neither an expert in prolog, nor in logic, but I'll try to explain what I think how it works.
After the call to:
<- conc(['a','b'],['c','d'],L).
Prolog will look for a predicate that matches the signature. In this case that would be:
conc([H|L1],L2,[H|L3])
It tries to resolve the variables with the given data.
|1: H:=['a'], L1:=['b'], L2:=['c','d']
Now it steps into the recursion with these data calling:
<- conc(['b'], ['c','d'], L3).
|2: H:=['b'], L1:=[], L2:=['c','d']
<- conc([], ['c','d'], L3).
The last line causes prolog to use the predicate with the signature:
conc([],L,L).
resolving:
|3: L:=['c','d']
Now Prolog is able to construct the concatinated List handing over L up the recursion stack.
|2: [H|L3]:=['b','c','d']
|1: [H|L3]:=['a','b','c','d']
I hope that is a hint in the right direction. Maybe you should read this article for clarification