-- Lecture 3B -- IO -- David Sands -- **** -- Small examples of IO -- putStr, writeFile -- * Building instructions vs running instructions -- instructions with results: -- Readfile -- putStr putStrLn -- building complex instructions from simple ones -- joining instructions, simple form: verboseWritefile :: FilePath -> String -> IO() verboseWritefile file contents = do putStrLn ("Writing the file " ++ file) putStrLn ("The contents are " ++ take 20 contents ++ "...") writeFile file contents putStrLn "Done!" -- Joining instructions, using results -- copyFile greeting :: IO() greeting = putStrLn "Hi" copyFile :: FilePath -> FilePath -> IO() copyFile fromFile toFile = do s <- readFile fromFile writeFile toFile s -- return lengthFile :: FilePath -> IO Int lengthFile file = do s <- readFile file return (length s) -- doTwice -- doNot -- find longest Word in the file "/usr/share/dict/words" dict :: FilePath dict = "/usr/share/dict/words"