Reputation: 17
So I have a record type:
data Box = Box { edge :: Char
, time1 :: [Int]
, time2 :: Int
}
deriving Eq
and this list over here:
list_of_boxes :: [Box]
How can I sum the values for time1
in each box of the list, then find which box gives the highest value for time1
?
Upvotes: 1
Views: 170
Reputation: 116139
This will sum every list only once:
import Data.List
import Data.Ord
snd . maximumBy (comparing fst) . map (\x -> (sum . time1 $ x , x)) $ list_of_boxes
The above can be shortened a bit using arrows:
import Control.Arrow
snd . maximumBy (comparing fst) . map (sum . time1 &&& id) $ list_of_boxes
Upvotes: 1
Reputation: 12070
This will work
maximumBy (compare `on` sum . time1) list_of_boxes
You will need to import Data.List
and Data.Function
.
Upvotes: 6