% A further example of using append to concatenate % two lists. append([], List, List). append([Head|Tail], List, [Head|Rest]) :- append(Tail, List, Rest). % | ?- append([the,cat],[sat,on,the,mat], R). % % R = [the,cat,sat,on,the,mat] % % yes % In the following example the predicate is called % in the --+ mode (two variables and an input list). % The ; key is used several times to % force a failure, this causes backtracking to % search for a different solution. Each re-done % solution is related to the previous solution by % reconsidering the last choice made. % To make the explanation clearer, % I will refer to the two cases of the append predicate % as the base case (the first) and the recursive case (the second) % The first result is simply the match against the first % (base) case. The second result is where backtracking causes a % reconsideration of the last choice. The alternative choice % is to use the second case. Thus the second result % is after one recursion using the % recursive case and finishing with the base case, % the third result is after two recursions finishing % with the base case, etc etc. % | ?- append(X, Y, [the, cat, sat, on, the, mat]). % % X = [] % Y = [the,cat,sat,on,the,mat] ; % % X = [the] % Y = [cat,sat,on,the,mat] ; % % X = [the,cat] % Y = [sat,on,the,mat] ; % % X = [the,cat,sat] % Y = [on,the,mat] % % yes % | ?-