title: How to group and count in haskell?
body: <p>Given a list (for instance <code>[1,2,2,3,3,4,5,6]</code>) how is it possible to group and count them according to bins/range? I would like to be able to specify a range, so that:</p>
  
  <p>Say range=2, and using the previous list, would give me <code>[1, 4, 2, 1]</code>, given that there's 1 0's or 1's, 4 2's or 3's, 2 4's or 5's and 1 6's or 7's.</p>
  
  <p>Say range=4, and using the previous list, would give me [5, 3], given that there's 5 0's or 1's or 2's or 3's, 3 4's or 5's or 6's or 7's.</p>
  
  <p>I have looked into <code>group</code> and <code>groupBy</code> but not found appropriate predicates, and also the histogram-fill library. The latter seems very nice to create bins, but I could not find out how to load data into those bins.</p>
  
  <p>How can I achieve this?</p>
  
  <p>My attempt on one of the suggestions below:</p>
  
  <pre><code>import Data.List 
  import Data.Function 
  
  quantize range n = n `div` range  
  
  main = print (groupBy ((==) `on` quantize 4) [1,2,3,4,2]) 
  </code></pre>
  
  <p>The output is [[1,2,3],[4],[2]] when it should have been [[1,2,2,3],[4]]. Both suggestions below works on sorted lists. </p>
  
  <pre><code>main = print (groupBy ((==) `on` quantize 4) (sort [1,2,3,4,2]))   
  </code></pre>