A quick search on Hoogle showed that there is no such function. On the other hand, it was replied that there is one in the split package, called chunksOf.
However, you can do it on your own
group :: Int -> [a] -> [[a]]
group _ [] = []
group n l
| n > 0 = (take n l) : (group n (drop n l))
| otherwise = error "Negative or zero n"
Of course, some parentheses can be removed, I left there here for understanding what the code does:
The base case is simple: whenever the list is empty, simply return the empty list.
The recursive case tests first if n is positive. If n is 0 or lower we would enter an infinite loop and we don't want that. Then we split the list into two parts using take and drop: take gives back the first n elements while drop returns the other ones. Then, we add the first n elements to the list obtained by applying our function to the other elements in the original list.

A quick search on Hoogle showed that there is no such function. On the other hand, it was replied that there is one in the split package, called chunksOf.
However, you can do it on your own
group :: Int -> [a] -> [[a]]
group _ [] = []
group n l
| n > 0 = (take n l) : (group n (drop n l))
| otherwise = error "Negative or zero n"
Of course, some parentheses can be removed, I left there here for understanding what the code does:
The base case is simple: whenever the list is empty, simply return the empty list.
The recursive case tests first if n is positive. If n is 0 or lower we would enter an infinite loop and we don't want that. Then we split the list into two parts using take and drop: take gives back the first n elements while drop returns the other ones. Then, we add the first n elements to the list obtained by applying our function to the other elements in the original list.

This function, among other similar ones, can be found in the popular split package.
> import Data.List.Split
> chunksOf 3 [1,2,3,4,5,6,7,8,9]
[[1,2,3],[4,5,6],[7,8,9]]
Split list into groups of n in Haskell - Code Review Stack Exchange
list - Nested chunksOf in Haskell? - Stack Overflow
Subdividing a list in haskell - Stack Overflow
list - split elements into groups haskell - Stack Overflow
I've come across some older Haskell code which refers to Data.List.chunk with apparent type signature Int -> [a] -> [[a]].
Hoogle doesn't turn up anything - the closest being Data.Text.chunksOf.
Is there a different/better alternative?
There are Data.List.Split.chunksOf and Data.List.Grouping.splitEvery implementations of this routine in specialized packages (and a number included in other application packages: search by Int -> [a] -> [[a]] signature on Hayoo).
I think splitEvery implementation is pretty elegant:
splitEvery :: Int -> [a] -> [[a]]
splitEvery _ [] = []
splitEvery n xs = as : splitEvery n bs
where (as,bs) = splitAt n xs
The ready made function chunksOf works very well. When tasked to create 3 elements in sublists with 11 elements in the source list, two elements will be in the last sublist of the result. The following function also includes trailers.
mklsts n = takeWhile (not.null) . map (take n) . iterate (drop n)
I use this as pairs with a 2 for n and no n parameter. Pairs rock.
Edit/Add 4/12/2018
The match up of iterate and splitOn is one made in hell. In the questioner above, placing splitOn in a lambda may have compounded the problems. It is possible to make splitOn work with iterate but you have to ditch the fst of the tuple produced. That defeats the entire purpose. It is way cleaner and easier to use drop n with iterate. The results are the same. That's what the preceding function does. Otherwise, it's the same idea.
Here is a novel way of producing the identical results using tails imported from Data.List in a list comprehension. It picks up stragglers, too.
ts n ls = [take n l|l<-init$tails ls,odd (head l)]
The parameters are size-of-sublist and list
Edit 4/17/2018
Well, since I had some time at work a list comprehension version that does not use tails, a recursive version and a map version.
ttx s ls=[take s.drop x$ls|x<-[0,s..s*1000]]
Recursive
tkn n []=[];tkn n xs=[take n xs]++(tkn n $ drop n xs)
Map
tp n ls=takeWhile(not.null)$ map(take n.flip drop ls) [0,n..]
The list comprehension is virtually infinite. Change [0,s..s*200] to [0,s..] for true infinity. The recursive is, of course, inherently infinite and the map function uses a big takeWhile (not.null) to end itself.
A function like nestedChunksOf can't be done directly in Haskell, at least not one which operates on normal lists. The depth of the list is part of the type, so you can't have an arbitrary depth specified by a parameter.
But what you can do is nest chunksOf.
If we define chunksOf like this:
chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf n xs = fxs : chunksOf n sxs
where (fxs, sxs) = splitAt n xs
We can then nest it:
Main> :l test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> chunksOf 3 [1,1,1,2,2,2,3,3,3,4,4,4]
[[1,1,1],[2,2,2],[3,3,3],[4,4,4]]
*Main> chunksOf 2 $ chunksOf 3 [1,1,1,2,2,2,3,3,3,4,4,4]
[[[1,1,1],[2,2,2]],[[3,3,3],[4,4,4]]]
I hope that accomplishes what you wanted!
As stated in the other answers, this can't be done directly as in Haskell you always need to know the type of an expression and thus distinguish between [a], [[a]] etc. However, using polymorphic recursion you can define a data type that allows such arbitrary nesting by wrapping each level in a constructor:
data NestedList a = Value a | Nested (NestedList [a])
deriving (Show)
So just Value is isomorphic to a, Nested (Value ...) is isomorphic to [a], double Nested to [[a]] etc. Then you can implement
chunksOf :: Int -> [a] -> [[a]]
...
nestedChunksOf :: [Int] -> [a] -> NestedList a
nestedChunksOf [] xs = Nested (Value xs)
nestedChunksOf (c:cs) xs = Nested (nestedChunksOf cs $ chunksOf c xs)
And indeed
print $ nestedChunksOf [3, 2] [1,1,1,2,2,2,3,3,3,4,4,4]
outputs
Nested (Nested (Nested (Value [[[1,1,1],[2,2,2]],[[3,3,3],[4,4,4]]])))
You can write a function for that yourself:
mySplit :: Int -> [a] -> [[a]]
mySplit n [] = []
mySplit n xs = (take n xs):(mySplit n (drop n xs))
Demo:
λ> mySplit 2 [1,2,3,4,5,6]
[[1,2],[3,4],[5,6]]
λ> mySplit 2 [1,2,3,4,5,6,7]
[[1,2],[3,4],[5,6],[7]]
Another way would be to use the split package:
λ splitEvery 3 ['a'..'z']
["abc","def","ghi","jkl","mno","pqr","stu","vwx","yz"]
First when asking such a question, specify the type signature you want! So you want to create (given a constant sublist-length) from a flat list a whole list of lists. That suggests the signature
splitOfLen :: Int -> [a] -> [[a]]
Before you start implementing, it's clever to see if anybody has done that before:
- Hoogle gives a whole lot of similar result, but none that really matches.
- Hayoo shows that the function has been implemented in a whole lot of libraries, but really just as a local helper.
If you want to do it yourself, you should start with splitAt (which splits off one prefix) and progress to do that while anything remains:
splitsOfLen l xs = case splitAt l xs of
(p, []) -> [p]
(p, r) -> p : splitsOfLen l r