'Divide list into sublists with specific sizes
I need to divide list in that way: first and last sublists contain 1 element, second and second last contain 2 elements and so on.. I wrote a simplier version of that predicate: I just put 1 element in first sublists, 2 in second and so on. How to write predicate that I need?
Below I put my code:
divide(List, ListOfParts):-
divide(List, 1, ListOfParts).
divide(List, N, [List]):-
length(List, Len), Len =< N, !.
divide(List, N, [FirstN|TailParts]):-
append(FirstN, Tail, List),
length(FirstN, N), !,
NextN is N+1,
divide(Tail, NextN, TailParts).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
