How would you SPLIT a List in pieces of specified length ? …. Here is how :
:- initialization(main). say(L) :- write(L), write('\n'). %%this illustrates how to pick part of a List firstN(Lst,N,FirstN) :- length(FirstN,N), append(FirstN,_Rest,Lst). split([],_,[]) :- !. split(Lst, N, [FirstN|Res]) :- length(FirstN, N), append(FirstN, Rest, Lst), !, split(Rest, N, Res). %%when the last piece is smaller than the requested size split(Lst, N, [Lst]) :- length(Lst, Len), Len < N. main :- L = [1,2,3,4,5,6], say(L), firstN(L,5,F), say(F), split(L,2,L2), say(L2), split(L,3,L3), say(L3), split(L,4,L4), say(L4). ---- [1,2,3,4,5,6] [1,2,3,4,5] [[1,2],[3,4],[5,6]] [[1,2,3],[4,5,6]] [[1,2,3,4],[5,6]]