# Split list

**URL:** <https://swi-prolog.discourse.group/t/split-list/4836>\
**Category:** Help!\
**Tags:** list\
**Created:** [January 3, 2022, 6:39pm UTC](https://swi-prolog.discourse.group/t/split-list/4836 "2022-01-03T18:39:06Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [April 16, 2022, 3:52pm UTC](https://swi-prolog.discourse.group/t/split-list/4836/18 "2022-04-16T15:52:28Z")

</div>

> [@Boris](#):
>
> `don't need string//1`

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

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

```

Slightly more elegant than previous:

```prolog
:- 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).

```

---

_[View the full topic](https://swi-prolog.discourse.group/t/split-list/4836)._
