[Haskell-cafe] bit of a noob question
Daniel Fischer
daniel.is.fischer at web.de
Sat Oct 24 18:45:28 EDT 2009
Am Sonntag 25 Oktober 2009 00:27:50 schrieb spot135:
> Ok maybe a noob question, but hopefully its an easy one.
>
> This is what I've got so far:
>
> test :: x->[a] -> (b,[b])
That can't be. The implementation below has type
Eq a => a -> [(a,b)] -> (a,[b])
> test x arrlist = let test1 = x
> a = filter (\n -> fst n == test1) arrlist
> test2 = map snd a
> in (test1, [test2])
>
> so basically I have a list say [(a,1),(a,2),(a,3),(b,1),(b,2)] etc
> So I give the function a x value (a or b) in this case and it return
> (a,[1,2,3])
>
> which is all gravy
>
> But,
> Is there a way that i dont have to supply the a or b ie i call the function
> and it gives me the list
> [(a,[1,2,3]),(b,[1,2])...
>
> I presume i need another layer of recursion but I cant figure out how to do
> it.
>
> Any help would be gratefully received :-)
If the type of the elements of your list belongs to Ord, you should take a look at sort,
sortBy (if only the first component belongs to Ord) and groupBy from Data.List.
With these functions you can do it pretty easily.
If the types don't belong to Ord but just to Eq, a recursion using partition (also from
Data.List) works well (but slower).
More information about the Haskell-Cafe
mailing list