import Control.Monad.State import Data.List get_length :: String -> IO Int get_length name = do contents <- readFile name return (length (lines contents)) update_best_with :: (String, Int) -> StateT (String, Int) IO () update_best_with (current_name, current_count) = do (best_name, best_count) <- get if current_count > best_count then put (current_name, current_count) else return () do_next :: String -> StateT (String, Int) IO () do_next name = do count <- get_length name update_best_with (name, count) longest_file :: [String] -> IO (String, Int) longest_file names = execStateT (sequence_ (map do_next names)) ("none", 0) main = do (name, count) <- longest_file ["/etc/hostname", "/etc/ntp.conf", "/etc/timezone"] putStrLn ("Longest file is " ++ name ++ " which is " ++ show count ++ " lines long")