-- 6. cvičení import System.Directory import Data.Char main = do putStrLn "Zadej cestu k souboru: " cesta <- getLine existuje <- doesFileExist cesta if existuje then readAndPrint cesta else putStrLn "Není cesta k souboru!" -- odstranění do notace main' = putStrLn "Zadej cestu k souboru: " >> getLine >>= \cesta -> doesFileExist cesta >>= \existuje -> if existuje then readAndPrint cesta else putStrLn "Není cesta k souboru!" query otazka = do putStrLn otazka odpoved <- getLine return (odpoved == "ano") readAndPrint :: String -> IO () readAndPrint cesta = do obsah <- readFile cesta putStrLn obsah readAndPrint' :: String -> IO () readAndPrint' cesta = readFile cesta >>= (\obsah -> putStrLn obsah) -- závorky nejsou povinné {- ^^^^^^^^^^^^^^ :: IO String ^^^^^^^^^^^^^^ :: IO () ^^^^^^^^^^^^^^^^^^^^^^^^^^ :: String -> IO () ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: IO () -} readAndPrint'' :: String -> IO () readAndPrint'' soubor = readFile soubor >>= putStrLn {- ^^^^^^ :: String === FilePath ^^^^^^^^ :: FilePath -> IO String ^^^^^^^^^^^^^^^ :: IO String ^^^^^^^^ :: String -> IO () ^^^ substituce a = String b = () ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: IO () -} {- znaky = getLine >>= putStrLn . filter isAlpha znaky = getLine >>= \x -> (putStrLn . filter isAlpha) x znaky = getLine >>= \x -> putStrLn (filter isAlpha x) znaky = do x <- getLine putStrLn (filter isAlpha x) -} znaky = do x <- getLine let z = filter isAlpha x putStrLn z {- operátory pro IO -- bind (>>=) :: IO a -> (a -> IO b) -> IO b (>>) :: IO a -> IO b -> IO b -} map' :: (a -> b) -> [a] -> [b] map' f s = [ f x | x <- s ] filter' :: (a -> Bool) -> [a] -> [a] filter' p s = [ x | x <- s, p x ] -- map f (filter p s) mapFilter :: (a -> b) -> (a -> Bool) -> [a] -> [b] mapFilter f p s = [ f x | x <- s, p x ] replicate' n x = [ x | _ <- [1..n] ] iterate' :: (a -> a) -> a -> [a] iterate' f x = x : iterate' f (f x) -- iterate (+1) 0 ~> 0 : iterate (+1) (0 + 1) ~> 0 : iterate (+1) 1 ~> 0 : 1 : iterate (+1) 2 ~> 0 : 1 : 2 : iterate (+1) 3 ~>... fib 0 = 0 fib 1 = 1 fib x = fib (x - 1) + fib (x - 2) fibs = [ fib x | x<-[0..] ] -- iteate vytovří seznam dvojic, následně bereme první složku fibs' = map fst (iterate (\(a, b) -> (b, a+b)) (0,1)) fibs'' = 0 : 1 : zipWith (+) fibs'' (tail fibs'') {- 0 : 1 : 1 : 2 : 3 : 5 : 7 : - 1 : 1 : 2 : 3 : 5 : 7 : - -------------------------- (+) - 1 : 2 : 3 : 5 : 7 : 12 -}