<div>Thank you all for the helpful responses! The solution from Daniel Fischer worked like a charm, the solution from Aai kinda worked, but I can&#39;t use because I don&#39;t want to modify the order of the elements in the list...</div>

<div><br></div><div>And yes, my &quot;criterion&quot; was a bit ill-defined, so the very clever function from Ozgur satisfied it :P The real goal is to maximize the number of groups though.</div><div><br></div><div>I&#39;ll test the solutions more hardly today and try to make the functions from Daniel even more clearer and elegant. I post the results back here!</div>

<div><br></div><div><br></div><div>Thanks a lot!</div><br clear="all">João Paulo Pizani Flor<div><a href="mailto:joaopizani@gmail.com" target="_blank">joaopizani@gmail.com</a></div><div>Federal University of Santa Catarina</div>


<br><br><div class="gmail_quote">2010/11/23 Daniel Fischer <span dir="ltr">&lt;<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">On Tuesday 23 November 2010 18:23:28, João Paulo Pizani Flor wrote:<br>
&gt; Hello dear Haskellers!<br>
&gt;<br>
&gt; I&#39;ve been a user and big fan of Haskell for a while, but only now I&#39;m<br>
&gt; writing my first &quot;big&quot; project in Haskell (some thousands of lines of<br>
&gt; code perhaps). It&#39;s an interpreter for a programming language, the<br>
&gt; source code is music! Yes, sheet music! :D<br>
&gt;<br>
&gt; OK, so my specific problem goes like this: I have a list of tuples<br>
&gt;<br>
&gt; :t  myList<br>
&gt;<br>
&gt; [ (Float, Integer) ]<br>
&gt;<br>
&gt; And I want to &quot;group&quot; this list into a nested list<br>
&gt; groupAtoms :: [ (Float,Integer) ]  -&gt;  [ [(Float,Integer)] ]<br>
&gt;<br>
&gt; Of course, I want that the concatenation of the groups yield me the<br>
&gt; original list, i.e,  (foldl (++) [] . groupAtoms == id),<br>
<br>
</div>Here, foldr is the better choice. (++) can start delivering its result<br>
before inspecting its second argument, hence<br>
<br>
foldr (++) [] (list1 : list2 : list3 : ...)<br>
  = list1 ++ (foldr (++) [] (list2 : list3 : ...))<br>
  = list1 ++ (list2 ++ (foldr (++) [] (list3 : ...)))<br>
<br>
can be consumed as it is constructed, so it can run in constant space, and<br>
it can stop early if not the entire result is needed. And it can deal with<br>
infinite lists.<br>
<br>
The prelude function concat does this.<br>
<br>
foldl on the other hand can&#39;t deliver anything before the whole input list<br>
has been traversed, in particular it doesn&#39;t terminate on infinite input,<br>
it needs at least O(length result) space and it can&#39;t stop early.<br>
Additionally, foldl (++) [] constructs left-nested (++)-applications, which<br>
are bad for performance.<br>
<div class="im"><br>
&gt; and the criterion that defines a group is that:<br>
&gt; &quot;The sum of the first elements in the tuples comprising the list must be<br>
&gt; greater than or equal to 1.0&quot;. That is, given a list of tuples, the<br>
&gt; boolean predicate deciding whether this list is a PROPER group (True) or<br>
&gt; TOO SMALL (False) is:<br>
&gt; \g -&gt; sum (map fst g)  &gt;=  1.0<br>
&gt;<br>
&gt;<br>
&gt; Although the criterion is very clear, I&#39;ve tried hard until now and<br>
&gt; couldn&#39;t come up with a function for producing the nested list based on<br>
&gt; this grouping criterion.<br>
<br>
</div>Split the task into two parts.<br>
<br>
1. collect the first group and the remainder of the list as a pair of lists<br>
2. cons the first group to what recursion on the remainder delivers<br>
<br>
Very much like the definition of `lines&#39;.<br>
<br>
For your use-case, the condition when a group is complete doesn&#39;t only<br>
depend on the list element but also on the previous, concretely the running<br>
sum of the first components.<br>
For a general such function, you need<br>
- the stopping condition<br>
- an accumulation parameter<br>
- an update function for the accumulating parameter<br>
as arguments.<br>
For example (stupid names, sorry)<br>
<br>
<br>
breakAcc :: (b -&gt; Bool) -&gt; (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; ([a],[a])<br>
breakAcc _    _    _   []       = ([],[])<br>
breakAcc cond comb acc xxs@(x:xs)<br>
    | cond acc                  = ([],xxs)<br>
    | otherwise                 = (x:ys,zs)<br>
      where<br>
        (ys, zs) = breakAcc cond comb (comb x acc) xs<br>
<br>
solves part 1, then<br>
<br>
<br>
groupsAcc :: (b -&gt; Bool) -&gt; (a -&gt; b -&gt; b) -&gt; b -&gt; [a] -&gt; [[a]]<br>
groupsAcc _    _    _   []  = []<br>
groupsAcc cond comb acc xs  = let (g,tl) = breakAcc cond comb acc xs<br>
                              in g : groupsAcc cond comb acc tl<br>
<br>
gives you part 2, and your particular function is<br>
<div class="im"><br>
<br>
groupAtoms :: [(Float,Integer)] -&gt; [[(Float,Integer)]]<br>
</div>groupAtoms = groupsAcc (&gt;= 1.0) ((+) . fst) 0<br>
<br>
<br>
Of course the last group need not be proper.<br>
If there are long groups, the above can cause a space leak (as lines had in<br>
GHC &lt; 7), that can be avoided by sacrificing a little non-strictness and<br>
replacing the let (g,tl) = ... in with a<br>
<br>
case ... of<br>
  (g,tl) -&gt; g : groupsAcc ...<br>
<br>
or, if you need the non-strictness, with a slightly convoluted method as<br>
used for lines in GHC 7.<br>
<div><div></div><div class="h5"><br>
&gt; I am sure that the Haskell Hierarchical<br>
&gt; Libraries have something to help me, but only now I see I&#39;m still a big<br>
&gt; noob :P<br>
&gt;<br>
&gt; Could someone please help me writing this function?<br>
&gt;<br>
&gt;<br>
&gt; My best regards from Brazil,<br>
&gt;<br>
&gt; João Paulo Pizani Flor<br>
&gt; <a href="mailto:joaopizani@gmail.com">joaopizani@gmail.com</a><br>
&gt; Federal University of Santa Catarina<br>
<br>
</div></div></blockquote></div><br>