Nowandthen98
Nowandthen98

Reputation: 300

Splitting a list into uneven sub-lists

I'm attempting to split a list of 50 items into different sized sub-lists; e.g. 3 of 10, 4 of 5.

I tried doing this using take but this just takes the first few items in the list repeatedly rather than moving along by index.

Does anyone have any advice on how to do this? Or can link me to a Prelude/Data.List function which may aid me. (I have looked around but I am struggling to find anything.)

Upvotes: 0

Views: 261

Answers (1)

sdx23
sdx23

Reputation: 168

A fitting combination of

splitAt :: Int -> [a] -> ([a], [a])

and

snd :: (a, b) -> b

should do the trick.

EDIT:

splitUp :: [Int] -> [a] -> [[a]]
splitUp [] xs = [xs]
splitUp _ [] = []
splitUp (l:ls) xs = let (a,b) = splitAt l xs in a : splitUp ls b 

Upvotes: 1

Related Questions