:- import append/3 from basics.
help1(0,L,[]).
help1(_,[],[]).
help1(N,[X|Xs],[X|Res]):- N2 is N - 1, help1(N2,Xs,Res).
help2(0,L,L).
help2(N,[X|Xs],Res):- N2 is N - 1, help2(N2,Xs,Res).
help3(N,L,R):- help1(N,L,R) append help2(N,L,R).
In the following piece of code my help1 predicate will store the first N values in a list. My help2 predicate will store all the values after the first N values in a list.
Finally in my help3 function i am trying to append the results i get from help1 and help2. But i am not able to do so. Can anyone help me out or point out what mistake i have done?
First, the definition of
help1
is too general:This can be fixed by removing the second clause in the predicate definition:
For concatenating two lists, use
append/3
. Take care of the argument order!Done! Let's try it:
One more thing... actually you don't need to define the auxiliary predicates
help1
andhelp2
.Simply use
append/3
andlength/2
like this:Sample use: