module Exercise_7 where import Data.List {-WETT-} filterFor :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] filterFor [] = [] filterFor ((xc, (x1, x2)):xs) = case find (\(yc, (y1, y2)) -> y1 == x1 && y2 == x2 && yc <= xc) xs of Just _ -> filterFor xs Nothing -> (xc, (x1, x2)):filterFor xs comp :: Eq b => [(Integer,(a,b))] -> [(Integer,(b,c))] -> [(Integer,(a,c))] comp [] _ = [] comp _ [] = [] comp xs ys = [(xn + yn, (fst xt, snd yt)) | (xn, xt) <- xs, (yn, yt) <- ys, snd xt == fst yt] trancl :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] trancl xs = (reverse . filterFor2 . sortBy (\(x1, _) (x2, _) -> compare x2 x1)) (multipleComp xs [] []) filterFor2 :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] filterFor2 [] = [] filterFor2 ((xc, (x1, x2)):xs) = case find (\(yc, (y1, y2)) -> y1 == x1 && y2 == x2 && yc <= xc) xs of Just _ -> filterFor xs Nothing -> (xc, (x1, x2)):filterFor xs multipleComp :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] -> [(Integer,(a,a))] -> [(Integer,(a,a))] multipleComp [] _ _ = [] multipleComp xs [] _ = multipleComp xs xs xs multipleComp xs ys acc = let currComp = filterFor3 acc (comp ys xs) in if currComp == [] then acc else (multipleComp xs currComp (acc ++ currComp)) filterFor3 :: Eq a => [(Integer,(a,a))] -> [(Integer,(a,a))] -> [(Integer,(a,a))] filterFor3 _ [] = [] filterFor3 [] ys = ys filterFor3 xs ((yc, (y1, y2)):ys) = case find (\(xc, (x1, x2)) -> x1 == y1 && x2 == y2 && xc < yc) xs of Just _ -> filterFor3 xs ys Nothing -> (yc, (y1, y2)):filterFor3 xs ys {-TTEW-}