Split list

If I use L instead of string(L), then this increases from 4 to 11 seconds:

?- numlist(1, 10000000, L), time(split_list(3, L, S)).

Slightly more elegant than previous:

:- use_module(library(dcg/basics)).

split_list(Len, Lst, LstSplit) :-
    must_be(positive_integer, Len),
    phrase(split(LstSplit, Len), Lst).

split([H|T], Len) --> list_length(H, Len), !, split(T, Len).
% Terminate at end of list
split([H], _) --> [H], !.
split([], _) --> [].

list_length(L, Len) --> { length(L, Len) }, string(L).
1 Like