import Prelude hiding (putStr,getLine) main1 = putStr "Hi" >> putStr "Hi" main2 = let x = putStr "Hi" in x >> x main3 = let actions = repeat (putStr "Hi") in actions!!3 >> actions!!99 -- Applies an I/O action twice: dupAct :: IO a -> IO a dupAct act = act >> act main4 = dupAct (putStr "Hi") -- Applies an I/O action multiple times: multAct :: Int -> IO a -> IO a multAct n act = foldr1 (>>) (take n (repeat act)) main5 = multAct 2 (putStr "Hi") fac :: Integer -> Integer fac n = if n==0 then 1 else n*fac(n-1) printFac10 :: IO () --printFac10 = putStr (show (fac 10)) printFac10 = print (fac 10) putStr :: String -> IO () putStr [] = return () putStr (c:cs) = putChar c >> putStr cs -- Reads a line from the keyboard getLine' :: IO String getLine' = getChar >>= \c -> if c=='\n' then return "" else getLine' >>= \cs -> return (c:cs) -- Reads a line from the keyboard getLine :: IO String getLine = do c <- getChar if c=='\n' then return "" else do cs <- getLine return (c:cs) -- Print intermediate result of factorial computation facPrint :: Integer -> IO Integer facPrint n = if n==0 then return 1 else do f1 <- facPrint(n-1) print (n-1, f1) return (n*f1) mainFac :: IO () mainFac = do putStr "n = " inp <- getLine f <- facPrint (read inp) putStrLn ("The factorial of " ++ inp ++ " is " ++ show f) -- Prints number of a lines of a file: printFileLines :: String -> IO () printFileLines f = readFile f >>= print . length . lines -- Prints number of a empty lines of a file: printFileELines :: String -> IO () printFileELines f = readFile f >>= print . length . filter (all (==' ')) . lines