import System(getArgs) main:: IO() main = do files <- getArgs places <- mapM fOpen files putStr $ output $ insertp $ map (filter nonemp) places data Place = FL FilePath Int String instance Eq Place where (FL _ _ str1) == (FL _ _ str2) = (str1 == str2) instance Ord Place where (FL _ _ str1) <= (FL _ _ str2) = (str1 <= str2) mkPlace:: FilePath ->(Int, String)->Place mkPlace a (b,c) = FL a b c nonemp :: Place -> Bool nonemp (FL _ _ []) = False nonemp (FL _ _ " ") = False nonemp _ = True fOpen :: FilePath->IO ([Place]) fOpen file = do str <- readFile file return $ map (mkPlace file) $ zip [1..] (lines str) insertp :: [[Place]] -> Tree insertp [] = error "no input?" insertp (x:xs) = insertp' xs (inserts x) insertp' :: [[Place]] -> Tree -> Tree insertp' [] tree = tree insertp' (x:xs) tree = insertp' xs (inserts' x tree) inserts :: [Place]->Tree inserts input = inserts' input mkTree inserts'::[Place] -> Tree -> Tree inserts' [] tree = tree inserts' (x:xs) tree = inserts' xs (insertTree x tree) data Tree = Node (Maybe Tree) (Maybe Tree) [Place] mkTree:: Tree mkTree = Node Nothing Nothing [] mkPTree :: Place -> Tree mkPTree place = Node Nothing Nothing [place] mkPMTree :: Maybe Tree -> Place -> Maybe Tree mkPMTree Nothing place = Just $ mkPTree place mkPMTree (Just t) place = Just $ insertTree place t insertTree :: Place -> Tree -> Tree insertTree place (Node Nothing Nothing []) = Node Nothing Nothing [place] insertTree place (Node left right (a:xs)) | (place == a) = Node left right (place:a:xs) | (place < a) = Node (mkPMTree left place) right (a:xs) | (place > a) = Node left (mkPMTree right place) (a:xs) insertTree _ (Node _ _ []) = error "tree problems" insertTree _ (Node _ _ (_:_)) = error "tree problems" output :: Tree -> String output (Node left right list) = (outputM left) ++ (outputl list) ++ (outputM right) outputM :: Maybe Tree -> String outputM (Just tree) = output tree outputM Nothing = "" outputl :: [Place] -> String outputl [] = "" outputl [_] = "" outputl (( FL fp line str):xs) = str ++ ": " ++ fp++":"++(show line) ++ (foldl outputP " " xs)++"\n" outputP :: String -> Place -> String outputP str (FL fp line _ ) =str++fp++": "++(show line)++" "