Ketil Z. Malde writes: : | classify :: Eq b => [a->b] -> [a] -> [[[a]]] | classify cs xs = ... | | where for each classifying function in cs, I would get the xs | partitioned accordingly. E.g. | | classify [fst,snd] [(1,0), (1,2), (2,0)] | | would yield | | [ [(1,0), (1,2)], [(2,0)] -- classified by `fst` | , [(1,0), (2,0)], [(1,2)]] -- classified by `snd` | | Now, obviously, the problem is that fst and snd, being passed in a | list, needs to be of the same type; this complicates classifying a | list of type [(Int,Bool)], for instance¹. : | Can somebody suggest a solution, or a place to look? Hi. Another way is to extend the role of the classifying functions, so that they go on to make the comparison after extracting the classification keys. Here's a naive implementation.
import Data.List(partition)
classifyBy :: (a -> a -> Bool) -> [a] -> [[a]] classifyBy eq [] = [] classifyBy eq (x:xs) = case partition (eq x) xs of (ys, zs) -> (x:ys):classifyBy eq zs
classifyBys :: [a -> a -> Bool] -> [a] -> [[[a]]] classifyBys eqs xs = [classifyBy eq xs | eq <- eqs]
fsteq x y = fst x == fst y sndeq x y = snd x == snd y
test = classifyBys [fsteq, sndeq] [(1, '0'), (1, '2'), (2, '0')]
In practice, you'd probably want an Ord-like comparison (a -> a -> Ordering) instead of the Eq-like one (a -> a -> Bool). Regards, Tom 'existential quantifier skeptic' ;-)