-- :l "Macintosh HD:Users:schauss:MSS:Vorlesungen:FP-1-2003:Hugsprogs:IOtutorialprogs.hs" -- :l "MSS/Vorlesungen/FP-1-2003/Hugsprogs/IOtutorialprogs.hs" -- :l "Macintosh HD:Users:ms:MSS:Vorlesungen:FP-1-2003:Hugsprogs:IOtutorialprogs.hs" -- :l "Macintosh HD/Users/schauss/MSS/Vorlesungen/FP-1-2003/Hugsprogs/IOtutorialprogs.hs" -- :l "MSS/Vorlesungen/FP-1-2003/Hugsprogs/IOtutorialprogs.hs" --- import IOExts import IO import System.IO.Unsafe -- import System.IO import Data.IORef --- type IO a = World -> (a, World) --- getChar :: IO Char --- putChar :: Char -> IO () --- (>>=) :: IO a -> (a -> IO b) -> IO b --- (>>) :: IO a -> IO b -> IO b -- main ::IO () echo:: IO () echo = getChar >>= putChar doubleEcho:: IO () doubleEcho = getChar >>= (\c -> putChar c >> putChar c) getTwoChars :: IO (Char, Char) getTwoChars = getChar >>= \c1 -> getChar >>= \c2 -> return (c1,c2) getTwoCharsSow = getChar >>= \c1 -> getChar >>= \c2 -> (putChar ('\n') >> print (c1,c2) >> return (c1,c2)) tutgetLine :: IO [Char] tutgetLine = getChar >>= \c -> if c == '\n' then return [] else tutgetLine >>= \cs -> return (c:cs) --putStrLn is die Standardfunktion putLine :: [Char] -> IO () putLine [] = return () putLine (c:cs) = putChar c >> putLine cs tutnix :: a-> IO () tutnix _ = return () echoLine = do {x<-getLine; putLine x} getLin2 = do { c <- getChar; if c == '\n' then return [] else do { cs <- getLin2; return (c:cs) } } -- control structures forever :: Monad m => m a -> m b forever a = a >> forever a repeatN :: (Monad a, Num b) => b -> a c -> a () repeatN 0 a = return () repeatN n a = a >> repeatN (n-1) a -- Aktion pro Element einer Liste for :: Monad m => [a] -> (a -> m c) -> m () for [] fa = return () for (n:ns) fa = fa n >> for ns fa while :: Monad m => m Bool -> m a -> m () while check a = do { b <- check; if b then a >> while check a else return () } -- print :: Show a => a -> IO () testprintZahlen = for [1..10] print --- for ns fa = sequence_ (map fa ns) -- sequence_ :: Monad a => [a b] -> a () -- sequence_ as = foldr (>>) (return ()) as -- sequence [] = return [] -- sequence (a:as) = do { r <- a; -- rs <- sequence as; -- return (r:rs) } ---data IORef a -- wie in ML ---newIORef :: a -> IO (IORef a) ---readIORef :: IORef a -> IO a ---writeIORef:: IORef a -> a -> IO () count:: Int -> IO Int count n = do { r <- newIORef 0; loop r 1 } where ----- loop :: IOref Int -> Int -> IO Int loop r i | i > n = readIORef r | otherwise = do { v <- readIORef r; writeIORef r (v+i); loop r (i+1)} --- do {x <- count 5; print x} ------------ getInt :: IO Int getInt = do {x <- getLine; return ((read x)::Int)} getBool :: IO Bool getBool = do {x <- getLine; return ((read x)::Bool)} get2Bool = do {a <- getBool; b <- getBool; print (a && b)} add2Zahlen :: IO Int add2Zahlen = do {e <- getInt; f <- getInt; return (e+f) } getMbInt :: IO (Maybe Integer) getMbInt = do {x <- getLine; if x == "" then (return Nothing) else (return (Just ((read x)::Integer)))} ---- addiert Integer, bis leere Eingabe, addnZahlen = do {e <- getMbInt; case e of Nothing -> (return 0) Just z -> do {sum <- addnZahlen; return (sum+z)}} addnZahlenpr::IO () addnZahlenpr = do {x<- addnZahlen; print ("Ergebnis = " ++ show ((x)::Integer))} ---- reklamiert Fehler sofort addnZahlen' = do {e <- getMbInt; case e of Nothing -> (return 0) Just z -> z `seq` do {sum <- addnZahlen'; return (sum+z)}} addnZahlen'pr = do {x<- addnZahlen'; print ("Ergebnis = " ++ show ((x)::Integer))} -------Files -- readFile :: Filepath -> IO String -- writeFile :: Filepath -> IO String -> IO () -- appendFile :: Filepath -> IO String -> IO () einlesetest = do {file <- readFile "Macintosh HD:Users:schauss:MSS:Vorlesungen:FP-1-2003:Hugsprogs:IOtutorialprogs.hs"; putStrLn file} einlesetestlaptop = do {file <- readFile "MSS:Vorlesungen:FP-1-2003:Hugsprogs:IOtutorialprogs.hs"; putStrLn file} einlesetestlaptop2 = do {file <- readFile "MSS/Vorlesungen/FP-1-2003/Hugsprogs/IOtutorialprogs.hs"; putStrLn file} testug = unsafePerformIO getChar