<P><FONT size=2 face=\"Courier New\">% Rotate a list N places to the left </FONT></P> <P><FONT size=2 face=\"Courier New\">% rotate(L1,N,L2) :- the list L2 is obtained from the list L1 by <BR>% rotating the elements of L1 N places to the left.<BR>% Examples: <BR>% rotate([a,b,c,d,e,f,g,h],3,[d,e,f,g,h,a,b,c])<BR>% rotate([a,b,c,d,e,f,g,h],-2,[g,h,a,b,c,d,e,f])<BR>% (list,integer,list) (+,+,?)</FONT></P> <P><BR><FONT size=2 face=\"Courier New\">% Split a list into two parts</FONT></P> <P><FONT size=2 face=\"Courier New\">% split(L,N,L1,L2) :- the list L1 contains the first N elements<BR>% of the list L, the list L2 contains the remaining elements.<BR>% (list,integer,list,list) (?,+,?,?)</FONT></P> <P><FONT size=2 face=\"Courier New\">domains<BR> list=symbol*<BR>predicates<BR> rotate(list,integer,list).<BR> length(list,integer).<BR> rotate_left(list,integer,list).<BR> append(list,list,list).<BR> split(list,integer,list,list).<BR>clauses</FONT></P> <P><FONT size=2 face=\"Courier New\">rotate(L1,N,L2) :- N >= 0, <BR> length(L1,NL1), N1 = N mod NL1, rotate_left(L1,N1,L2).<BR>rotate(L1,N,L2) :- N < 0,<BR> length(L1,NL1), N1 = NL1 + (N mod NL1), rotate_left(L1,N1,L2).</FONT></P> <P><FONT size=2 face=\"Courier New\">rotate_left(L,0,L).<BR>rotate_left(L1,N,L2) :- N > 0, split(L1,N,S1,S2), append(S2,S1,L2).</FONT></P> <P><FONT size=2 face=\"Courier New\">append([],Listb,Listb).<BR> <BR> append([X|List1],List2,[X|List3]):-</FONT></P> <P><FONT size=2 face=\"Courier New\"> append(List1,List2,List3).</FONT></P> <P><FONT size=2 face=\"Courier New\">split(L,0,[],L).<BR>split([X|Xs],N,[X|Ys],Zs) :- N > 0, N1 = N - 1, split(Xs,N1,Ys,Zs).</FONT></P> <P><FONT size=2 face=\"Courier New\">length([],0).<BR>length([_|L],N) :- length(L,N1), N = N1 + 1.</FONT></P> <P><FONT size=2 face=\"Courier New\">--------------------------------------------------------</FONT></P> <P><FONT size=2 face=\"Courier New\">Goal: rotate([a,b,c,d,e,f,g,h],3,X)</FONT></P> <P><FONT size=2 face=\"Courier New\">X=([\"d\",\"e\",\"f\",\"g\",\"h\",\"a\",\"b\",\"c\"])</FONT></P> <P><FONT size=2 face=\"Courier New\">1 solution</FONT></P>