I need to split a list into equal sublists, e. g [1..9]
split into groups of 3 will be [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
. I have accomplished this task in the following way:
splitInGroupsOf n = takeWhile ((n ==) . length)
. map fst
. drop 1
. iterate (\(res, list) -> splitAt n list)
. (,) []
where iterate creates list of tuples with first n
elements and rest of list. This way I had to use (,) []
on argument to ensure correct type, and unwrap result afterwards. My questions are
- is there a better/more elegant way of performing same task?
- is there some standard function I should make use of?
P.S.: I'm not sure where to ask simple Haskell-related questions and will appreciate if someone points me a better place for this than SE.