[Haskell-cafe] Re: [Haskell-beginners] Problem "grouping" a list

Aai bradypus at xs4all.nl
Wed Nov 24 03:59:38 EST 2010


I don't know what this tuple is representing, but if you want to group you'll 
have to specify on 'what':
- the tuple,
- the fst or
- the snd

Here's a possibility with grouping on the fst

import Data.List
import Data.Ord
import Data.Function

groupAtoms ::
   (Float -> Bool)
   -> [(Float,Integer)]
   ->  ([[(Float,Integer)]],[[(Float,Integer)]])
groupAtoms p = partition (p.sum. map fst). groupBy ((==)`on`fst). sortBy 
(comparing fst)

Use:

groupAtoms (>=1.0) myList

If this is what you want: proper lists are in the fst of the result.





Hallo João Paulo Pizani Flor, je schreef op 23-11-10 18:23:
> Hello dear Haskellers!
>
> I've been a user and big fan of Haskell for a while, but only now I'm writing 
> my first "big" project in Haskell (some thousands of lines of code perhaps). 
> It's an interpreter for a programming language, the source code is music! Yes, 
> sheet music! :D
>
> OK, so my specific problem goes like this: I have a list of tuples
> :t  myList
> [ (Float, Integer) ]
>
> And I want to "group" this list into a nested list
> groupAtoms :: [ (Float,Integer) ]  ->  [ [(Float,Integer)] ]
>
> Of course, I want that the concatenation of the groups yield me the original 
> list, i.e,  (foldl (++) [] . groupAtoms == id), and the criterion that defines 
> a group is that:
> "The sum of the first elements in the tuples comprising the list must be 
> greater than or equal to 1.0". That is, given a list of tuples, the boolean 
> predicate deciding whether this list is a PROPER group (True) or TOO SMALL 
> (False) is:
> \g -> sum (map fst g) >=  1.0
>
>
> Although the criterion is very clear, I've tried hard until now and couldn't 
> come up with a function for producing the nested list based on this grouping 
> criterion. I am sure that the Haskell Hierarchical Libraries have something
> to help me, but only now I see I'm still a big noob :P
>
> Could someone please help me writing this function?
>
>
> My best regards from Brazil,
>
> João Paulo Pizani Flor
> joaopizani at gmail.com <mailto:joaopizani at gmail.com>
> Federal University of Santa Catarina
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-- 
Met vriendelijke groet,
=@@i

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20101124/dedae9b7/attachment.html


More information about the Haskell-Cafe mailing list