Kapitel 4: Ein- und Ausgabe hugs InOut ghc InOut.lhs > import Prelude hiding (sequence_, sequence, either) > import IO hiding (try) > import System < data IO val < putChar :: Char -> IO () < putStr :: String -> IO () < putStrLn :: String -> IO () < print :: (Show a) => a -> IO () < getChar :: IO Char < getLine :: IO String < readLine :: (Read a) => IO a < type FilePath = String < < readFile :: FilePath -> IO String < writeFile :: FilePath -> String -> IO () < appendFile :: FilePath -> String -> IO () < return :: a -> IO a < (>>) :: IO a -> IO b -> IO b < (>>=) :: IO a -> (a -> IO b) -> IO b > main1 = print [ (i, i ^ 2) | i <- [0 .. 9] ] > askFor1 :: String -> IO String > askFor1 s = putStr s >> getLine > > main2 :: IO () > main2 = askFor1 "filename: " >>= \file -> > readFile file >>= \cnts -> > putStr cnts |do|-Notation. > askFor2 :: String -> IO String > askFor2 s = do putStr s > getLine > > main3 :: IO () > main3 = do file <- askFor2 "filename: " > cnts <- readFile file > putStr cnts > sequence_ :: [IO a] -> IO () > sequence_ [] = return () > sequence_ (a : as) = a >> sequence_ as > > sequence :: [IO a] -> IO [a] > sequence [] = return [] > sequence (a : as) = a >>= \v -> > sequence as >>= \vs -> > return (v : vs) > askForMany :: [String] -> IO [String] > askForMany xs = sequence [ askFor s | s <- xs ] Standardbibliothek System. < data ExitCode = ExitSuccess | ExitFailure Int < < getArgs :: IO [String] < getProgName :: IO String < getEnv :: String -> IO String < system :: String -> IO ExitCode < exitWith :: ExitCode -> IO a Fehlerbehandlung. < data IOError < < userError :: String -> IOError < < ioError :: IOError -> IO a < catch :: IO a -> (IOError -> IO a) -> IO a < < fail :: String -> IO a < fail s = ioError (userError s) Die Standardbibliothek |IO|. < isAlreadyExistsError :: IOError -> Bool < isDoesNotExistError :: IOError -> Bool < isAlreadyInUseError :: IOError -> Bool < isFullError :: IOError -> Bool < isEOFError :: IOError -> Bool < isIllegalOperation :: IOError -> Bool < isPermissionError :: IOError -> Bool < isUserError :: IOError -> Bool Abgeleitete Operationen. > try :: IO a -> IO (Either IOError a) > try p = (p >>= (return . Right)) > `catch` (return . Left) > > either :: (a -> c) -> (b -> c) -> (Either a b) -> c > either f g (Left x) = f x > either f g (Right x) = g x > askFor :: String -> IO String > askFor s = putStr s >> > getLine `catch` \ err -> > if IO.isEOFError err then exitWith ExitSuccess > else ioError err > > main4 = askFor "filename: " >>= \file -> > try (readFile file) >>= > either > (\ err -> if IO.isDoesNotExistError err > then putStrLn "file does not exist" > else ioError err) > (\ cnts -> putStr cnts) > main = getArgs >>= cat > cat :: [String] -> IO () > cat [] = return () > cat ("-n" : x) = sequence_ [ copy True f | f <- x ] > cat x = sequence_ [ copy False f | f <- x ] > copy :: Bool -> FilePath -> IO () > copy b f = do cnts <- readFile f > sequence_ [ > do when b (putLineNo n) > putStrLn l > | (n, l) <- zip [1 .. ] (lines cnts) ] > putLineNo :: Int -> IO () > putLineNo n = putStr (rjustify 6 (show n) ++ " ") > when :: Bool -> IO () -> IO () > when b a = if b then a else return () > rjustify :: Int -> String -> String > rjustify n s = replicate (n - length s) ' ' ++ s