[Haskell-cafe] bit of a noob question

spot135 aca08sas at shef.ac.uk
Sat Oct 24 19:09:48 EDT 2009




Daniel Fischer-4 wrote:
> 
> 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).
> 
> 
> Thanks Daniel for the speedy reply :jumping:
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/bit-of-a-noob-question-tp26043671p26043923.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list