Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A quick search on <a href="http://www.haskell.org/hoogle/?hoogle=Int+-%3E+%5Ba%5D+-%3E+%5B%5Ba%5D%5D" rel="noreferrer">Hoogle</a> showed that there is no such function. On the other hand, it was replied that there is one in the <code>split</code> package, called <code>chunksOf</code>.</p> <p>However, you can do it on your own</p> <pre><code>group :: Int -&gt; [a] -&gt; [[a]] group _ [] = [] group n l | n &gt; 0 = (take n l) : (group n (drop n l)) | otherwise = error "Negative n" </code></pre> <p>Of course, some parentheses can be removed, I left there here for understanding what the code does:</p> <p>The base case is simple: whenever the list is empty, simply return the empty list.</p> <p>The recursive case tests first if <code>n</code> is positive. If <code>n</code> is <code>0</code> or lower we would enter an infinite loop and we don't want that. Then we split the list into two parts using <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:take" rel="noreferrer"><code>take</code></a> and <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:drop" rel="noreferrer"><code>drop</code></a>: <code>take</code> gives back the first <code>n</code> elements while <code>drop</code> returns the other ones. Then, we add the first <code>n</code> elements to the list obtained by applying our function to the other elements in the original list.</p> <p><img src="https://i.stack.imgur.com/voD4C.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload