Skip to content

Instantly share code, notes, and snippets.

@trietptm
Created December 13, 2012 18:51
Show Gist options
  • Save trietptm/4278660 to your computer and use it in GitHub Desktop.
Save trietptm/4278660 to your computer and use it in GitHub Desktop.
Split a list into two parts; the length of the first part is given. Do not use any predefined predicates. Example: ?- split([a,b,c,d,e,f,g,h,i,k],3,L1,L2). L1 = [a,b,c] L2 = [d,e,f,g,h,i,k]
length_list([], 0).
length_list([H|T], X) :- length_list(T, X1), X is X1+1, !.
split_list([X|Y], 1, [X], Y).
split_list([H|T], M, [H|L1], L2) :- split_list(T, M1, L1, L2), M is M1+1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment