Well, I'm pretty new to Haskell and what i basically want to do is this:
I have a list of tuples (they are type Int is that is important):
[((a,b),c),...,((x,y),z)]
i want to find the maximum of the c part and then return the (a,b) part corresponding to that.
I have tried to go like:
maximum (map snd [list])
This will give me the maximum of the second part but how do i then get the first part back again?
Any help would be awesome! :)
Mark
Lists of Tuples in Haskell
Started by markyp, May 15 2010 06:47 PM
2 replies to this topic
#1
Posted 15 May 2010 - 06:47 PM
|
|
|
#2
Posted 16 May 2010 - 08:58 PM
Go through each element in the list using foldl, and compare each second element. Then return the first one that is of the largest second element. Here's how I'd do it:
foldl is extremely useful for going through lists and selecting one element out of many. For example, this is similar to how you'd get the largest element in a list.
I hope this was at least somewhat useful. :)
goThroughList :: [((Int, Int), Int)] -> (Int, Int) goThroughList = foldl (\x y -> if snd x > snd y then fst x else fst y) ((0, 0), minIntValue)
foldl is extremely useful for going through lists and selecting one element out of many. For example, this is similar to how you'd get the largest element in a list.
I hope this was at least somewhat useful. :)
Wow I changed my sig!
#3
Posted 17 May 2010 - 11:43 PM
Thanks a lot Zeke.


Sign In
Create Account

Back to top









