module Exercise_7 where import Data.List import Data.Maybe {-WETT-} compMaybe :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] -> Maybe ([(Integer,(a,a))], [(Integer,(a,a))]) compMaybe rel1 rel2 = if res == rel2 then Nothing else Just (res, res) where res = unionBy (\tup1 tup2 -> snd tup1 == snd tup2 && fst tup1 <= fst tup2) rel2 [(weight1 + weight2, (fst tup1, snd tup2)) | (weight1, tup1) <-rel1, (weight2, tup2) <- rel2, snd tup1 == fst tup2] trancl :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] trancl tups = nubBy (\tup1 tup2 -> snd tup1 == snd tup2) (sortOn fst (concat (unfoldr (compMaybe tups) tups))) {-TTEW-}